代码改变世界

python 第三天

2018-08-17 14:14  小于漫谈  阅读(187)  评论(0编辑  收藏  举报

1、集合

集合是无序的,不重复的数据组合,集合的表示如下:

list_1 = set(list_1)

1.1、交集是两个列表都有元素

list_1 & list_2

1.2、并集是两个列表合并起来并取出重复的元素

list_1 | list_2

1.3、差集是list_1减去和list_2中共有的元素,意思就是我这有你没有的元素

list_1 - list_2

1.4、对称差集是:list_1和list_2中相同的元素去掉

list_1 ^ list_2

1.5、集合中加元素:

list_1.add(999)  #加一个元素      list_1.update([888,777,555])集合中加很多元素

1.6、集合中删元素:

list_1.remove(999)

1.7、测试元素是否在集合中:

x in s         x no in s

2、文件

2.1、打开文件,变成文件句柄

f = open("test",'r',encoding="utf-8") 只读方式打开

f = open("test",'r+',encoding="utf-8")  读写:可读,可追加

f = open('test','w',enconding="uft-8")  只写模式,文件没有就创建,有的话会直接覆盖掉原有文件的写方式

f = open('test','w+',enconding="uft-8")  写读,还是会有覆盖情况

f = open('test','a',enconding='utf-8')   只写模式,文件没有就创建,不会覆盖的追加模式

2.2、读文件

f.read()   会将整个文件读到内存中,大文件不适合

print(f)  打印出整个文件

first_line = f.readline()   打印出一行

f.close()  关闭文件

2.3、大文件读出来,不用.read()方法

可以将大文件写一行到内存中,读一行出来,经文件变成迭代器:

for line in f:

    print(line.strip())

2.4、在打印的时候,第10行输出标注

count = 0

for line in f:

     if count == 9:

         print(‘-----------分割线------------’)

         count += 1

         continue    #continue下边的代码不会执行,直接跳转到for语句

                          #break语句,打破了最小封闭for或while循环

     print(line.strip())

      count +=1

2.5、文件指针

f.seek()   当文件读完之后,指针就会知道内存中最后的位置,可以通过f.seek(0)将指针重新到头来第一个字符的位置

f.seek(10)  

f.tell()  指针现在位置

f.flush()  强制刷新,从内存到硬盘

f.read(5)  读出前5个字符

2.6、文件修改,shell sed方法,原理:对原有文件内容进行更改,然后写到新的文件中。

import sys

f = open("test",'r',encoding='utf-8')

f_new = open("test1",'w',endcong='utf-8')

find_str = sys.argv[1]

replace_str = sys.argv[2]

for line in f:

      if "hello" in line:

    line = line.replace(find_str,replace_str)

  f_new.write(line)

f.close()

f_new.close()

从以上看起来,文件的修改貌似只有COPY到另一个文件了,那么用r+是不是就可以解决了。

2.7 、with open打开文件

with open('test','r',encoding='utf-8')  as f:

  for line in f:

    print(line.strip())

当wIth代码执行完毕时,内部会自动关闭并释放文件资源。

with open("test") as f, open('test1') as f1:

  pass

可以同时打开多个文件。

3、函数

3.1、函数就是模板,可以调用

def func1()

  print("in the func1")

  return 0

x=func1()

print("form func1 return is %s" %x)

执行完之后,x是0,因为return返回的是0

3.2、带时间戳的文件读写,函数里调用函数

import time

def logger():

  time_format = '%Y-%m-%d %X'

  time_current = time.strftime(time_format)

  with open('a.txt','a+') as f:

    f.write('%s and action\n' %time_current)

def test1():

  print("test1 starting action..../n")

  logger()

3.3、函数中其实返回的是一个元组,可以返回很多元素,但是会变成一整个元组

def test():

  print('in the test3')

  return 1, 'hello',['world','test2'],{'name':'alex'}

x=test()

3.4、函数中与形参顺序无关,同时关键参数是不能写到位置参数前面,关键参数比如就是 y=2,位置参数就是字符串

def test(x,y)

  print(x)

  print(y)

test(2,y=1)   #y=1是不能写到2之前的

3.5、参数传入

def test(*args)  #此种函数只接收位置参数,转换成元组

  print(args)

test(1,3)    结果  (1,3)

test([1,2,2,4,5])  结果 ([1,2,2,4,5])

通过以上可知里边的元素,直接就变成了外挂一个括号

def test2(**kwargs):

  print(kwargs)

  print(kwargs['name'])

  print(kwargs['age'])

  print(kwargs['sex'])

test2(name='alex',age=22,sex='F')

输入结果是一个字典,字典可以调用相关的KEY,出来VALUE

def test4(name,age=18,**kwargs):

  print(name)

  print(age)

  print(kwargs)

  logger("TEST4")

def logger(source):

  print("from %s" %source)

test4('alex',sex='m',hobby='tesla',age=3)

3.6、递归

在函数内部,可以调用其他函数,如果一个函数在内部调用自身,这个函数就叫递归函数

def calc(n):

  print(n)

  return calc(n+1)

calc(0)   为了防止内存溢出,最多递归999次,意思输出到998就结束了

def calc(n):

  print(n)

  if int(n/2) > 0:

    retrun calc(int(n/2))

  print("->",n)

calc(10)