一、集合:
list_1 = [1,4,5,7,3,6,7,9]
list_1 = set(list_1)
list_2 = set([2,6,0,66,22,8,4])
#交集
list_1.intersection(list_2) #list_1 & list_2
#并集
list_1.union(list_2) #list_1 | list_2
#差集
list_1.difference(list_2) #list_1 - list_2
list_2.difference(list_1) #list_2 - list_1
list_3 = set([1,3,7])
#子集
list_3.issubset(list_1) #list_3 <= list_1
#父集
list_1.issuperset(list_3) #list_1 >= list_3
#对称差集
list_1.symmetric_difference(list_2) #list_1 ^ list_2
list_4 = set([5,6,8])
list_3.isdisjoint(list_4) #没有交集返回Ture
list_1.add(999) #添加
list_1.update([888,777,555])
list_1.remove(4) #删除
list_1.discard('ddd')
len(list_1) #取长
二、文件:
#读
f = open("yesterday","r",encoding="utf-8")
data = f.read()
f.close()
#写
f = open("yesterday2","w",encoding="utf-8")
f.write("我爱北京*\n")
f.write("*上太阳升")
f.close()
#读前5行
f = open("yesterday","r",encoding="utf-8")
for i in range(5):
line = f.readline()
f.close()
#读每一行
f = open("yesterday","r",encoding="utf-8")
for index,line in enumerate(f.readlines()):
if index == 9:
print('---------------------------')
continue
print(line.strip())
#高效率读
for line in f:
print(line)
f.close()
f = open("yesterday","r",encoding="utf-8")
print(f.tell())
f.read(5)
print(f.tell()) #打印光标位置
f.seek(0) #光标移到开始位置
f.close()
import sys,time
for i in range(20):
sys.stdout.write('#')
sys.stdout.flush()
time.sleep(0.2)
三、函数
#递归
def calc(n):
print(n)
if int(n/2)>0:
return calc(int(n/2))
print("->",n)
calc(10)
#高阶函数
def add (a,b,f):
return f(a)+f(b)
res = add(3,-6,abs)
print(res)