一.关系运算
有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
linuxs={'wupeiqi','oldboy','gangdan'}
1. 求出即报名python又报名linux课程的学员名字集合
print(pythons & linuxs)
2. 求出所有报名的学生名字集合
print(pythons | linuxs)
3. 求出只报名python课程的学员名字
print(pythons - linuxs)
4. 求出没有同时这两门课程的学员名字集合
print(pythons ^ linuxs)
二.去重
1. 有列表l=['a','b',1,'a','a'],列表元素均为可hash类型,去重,得到新列表,且新列表无需保持列表原来的顺序
l=['a','b',1,'a','a'] li = set(l) print(li)
2.在上题的基础上,保存列表原来的顺序
l=['a','b',1,'a','a'] li = [] for i in l: if i not in li: li.append(i) print(li)
3.去除文件中重复的行,肯定要保持文件内容的顺序不变
方法一:
1 import os 2 with open('db.txt',mode='w',encoding='utf-8') as f: 3 f.write('''我也不知道要写什么 4 反正要有重复的行 5 那就这行吧 6 那就这行吧 7 完毕 8 ''') 9 with open('db.txt',encoding='utf-8') as read_f,\ 10 open('db1.txt',mode='w',encoding='utf-8') as write_f: 11 li = [] 12 for line in read_f.readlines(): 13 if line not in li: 14 li.append(line) 15 write_f.write(line) 16 os.remove('db.txt') 17 os.replace('db1.txt','db.txt')
方法二:
1 with open('db.txt',mode='w',encoding='utf-8') as f: 2 f.write('''我也不知道要写什么 3 反正要有重复的行 4 那就这行吧 5 那就这行吧 6 完毕 7 ''') 8 with open('db.txt',encoding='utf-8') as f1: 9 li = [] 10 for line in f1.readlines(): 11 if line not in li: 12 li.append(line) 13 with open('db.txt',mode='w',encoding='utf-8') as f2: 14 for j in li: 15 f2.write(j)
4.有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序
l=[
{'name':'egon','age':18,'sex':'male'},
{'name':'alex','age':73,'sex':'male'},
{'name':'egon','age':20,'sex':'female'},
{'name':'egon','age':18,'sex':'male'},
{'name':'egon','age':18,'sex':'male'},
]
1 l=[ 2 {'name':'egon','age':18,'sex':'male'}, 3 {'name':'alex','age':73,'sex':'male'}, 4 {'name':'egon','age':20,'sex':'female'}, 5 {'name':'egon','age':18,'sex':'male'}, 6 {'name':'egon','age':18,'sex':'male'}, 7 ] 8 li = [] 9 for i in range(0,len(l)): 10 if l[i] not in li: 11 li.append(l[i]) 12 print(li)