py20180828


集合操作
list_1 = [1,4,5,7,3,6,7,9]
list_1 = set(list_1) #列表设置为集合,集合是无序的
list_2 = set([2,6,0,22,66,8,4])
list_3 = set([1,4,5])
print( list_1.intersection(list_2) ) #交集b = t & s ------ t 和 s的交集
print( list_2.union(list_1) ) #并集a = t | s ----- t 和 s的并集
print( list_1.difference(list_2) ) #差集(1有2没有)c = t – s ------ 求差集(项在t中,但不在s中
print( list_3.issubset(list_1) ) #子集判断1位2 的子集
print( list_3.issuperset(list_1) ) #父集
print( list_1.symmetric_difference(list_2) ) #对称差集d = t ^ s ------对称差集(项在t或s中,但不会同时出现在二者中)
print( list_1.isdisjoint(list_2)) #如果存在交集返回false否则返回true

list_1.add(999) #增加
list_1.update([888,777]) #增加多个
list_1.remove(777) #删除对象不存在报错
print(list_1.discard(222)) #删除对象不存在不会报错
print(list_1)


文件操作
#date = open("yesterday",encoding='utf-8').read()  #打开文件同时指定字符编码,默认操作系统字符编码
#file = open("yesterday",'r',encoding= 'utf-8')#读模式打开文件--文件句柄
file = open('yesterday2','a',encoding= 'utf-8')#写模式打开文件(创建一个文件)
#r 读 w 写 a 追加
# date = file.read()
# date2 = file.read()
# print(date)
# print('-------------',date2)
file.write('python 自动化之路,\n')
file.write('python 自动化之路')
file.close()


猜你喜欢

转载自www.cnblogs.com/cly0205/p/9551543.html
py
今日推荐