list_101 = [1,2,3,4,5,6,7] list_101 = set(list_101) list_102 = [2,3,4,5,6,7,8,9,10,11,12,13,15,14] list_102 = set(list_102) #交集 intersection and & print(list_101.intersection(list_102)) print(list_101 & list_102) #并集 union | print(list_101.union(list_102)) print(list_101 | list_102) #差集 fifference - print(list_101.difference(list_102)) print(list_102.difference(list_101)) print(list_101 - list_102) #对称差集 ^ print(list_101.symmetric_difference(list_102)) print(list_101 ^ list_102) #增加 add list_101.add(1024) print(list_101) #添加多项 list_101.update(["a","b","c","d"]) print(list_101) #删除 list_101.remove(1) #remove 找不到目标会报错 print(list_101) #删除 list_101.discard(1024) #discard 删除选项如果找不到目标、不会报错 print(list_101) #判断字符是否在合集中 print(2 in list_101) print(1111 not in list_101) #文件操作 #读 ‘‘‘ f = open("1024",‘r‘,encoding="utf-8") data = f.read() print(data) f.close() #写 f_1 = open("1024",‘w‘,encoding="utf-8") f_1.write("我爱北京天安门,天安门上太上升!\n") f_1.write("伟大领袖毛主席安,带领我们向前进\n") #这样写入会把文档中的所有文件覆盖 f_1.close() #追加 f_2 = open("1024",‘a‘,encoding="utf-8") f_2.write("我爱北京天安门!!!!!!!!\n") f_2.write("伟大领袖毛主席,带领我们向前进!!!!!!!!!\n") ‘‘‘ #进度条 import sys import time for i in range(100): sys.stdout.write("#") sys.stdout.flush() time.sleep(0.1)
时间: 2024-10-20 01:32:11