1、有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
linuxs={'wupeiqi','oldboy','gangdan'}
1. 求出即报名python又报名linux课程的学员名字集合
2. 求出所有报名的学生名字集合
3. 求出只报名python课程的学员名字
4. 求出没有同时这两门课程的学员名字集合
# 1. 求出即报名python又报名linux课程的学员名字集合 print(pythons & linuxs) # 2. 求出所有报名的学生名字集合 print(pythons | linuxs) # 3. 求出只报名python课程的学员名字 print(pythons - linuxs) # 4. 求出没有同时这两门课程的学员名字集合 print(pythons ^ linuxs)
2、有列表l=['a','b',1,'a','a']
1、去重,得到新列表,且新列表无需保持列表原来的顺序
2、去重,得到新列表,且新列表需要保持原来的顺序
# 第一题答案 l = set(l) # 第二题答案 List = [] for i in l: if i in List: continue else: List.append(i) l = List
3、有如下文件.db.txt,请去除文件中重复的行
我也不知道要写什么
反正要有重复的行
那就这行吧
那就这行吧
完毕
import os with open('db.txt','r',encoding='utf-8') as read_f, open('db1.txt','w',encoding='utf-8') as write_f: li = [] for line in read_f.readlines(): if line not in li: li.append(line) write_f.write(line) os.remove('db.txt') os.rename('db1.txt','db.txt')
import os with open('db.txt','r',encoding='utf-8') as read_f: li = [] for i in read_f: if i not in li: li.append(i) with open('db.txt','w',encoding='utf-8') as write_f: for i in li: write_f.write(i)
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'},
]
List = [] for i in l: if i not in List: List.append(i)
5、文件a.txt内容:每一行内容分别为商品名字,价钱,个数,求出本次购物花费的总钱数
apple 10 3
tesla 100000 1
mac 3000 2
lenovo 30000 3
chicken 10 3
with open('a.txt',encoding='utf-8') as read_f: num1 = 0 num2 = 0 money = 0 for line in read_f.readlines(): num1 = int(line.split(' ')[1]) num2 = int(line.split(' ')[2]) money += num1*num2 print(money)
6、修改文件内容,把文件a.txt中的alex都替换成SB
SB 18
Eva 20
KID 21
kidd 22
SB 38
with open('a.txt') as file: List = [] for i in file.readlines(): List.append(i) with open('a.txt','w') as file: for i in List: if 'alex' in i: file.write(i.replace('alex','SB')) else: file.write(i)
7、有如下文件,a1.txt
老男孩是最好的学校,
全心全意为学生服务,
只为学生未来,不为牟利。
我说的都是真的。哈哈
分别完成以下的功能:
每天坚持一点,
每天努力一点,
每天多思考一点,
慢慢你会发现,
你的进步越来越大。
e,将原文件内容全部读取出来,并在‘我说的都是真的。哈哈’这一行的前面加一行,‘你们就信吧~’然后将更改之后的新内容,写入到一个新文件:a1.txt。
with open('a1.txt','r',encoding='utf-8') as F1: print(F1.read())
with open('a1.txt','a',encoding='utf-8') as f1: f1.write(' 信不信由你,反正我信了。')
with open('a1.txt','r',encoding='utf-8') as f1: print(f1.read()) print('信不信由你,反正我信了。')
with open('a1.txt','w',encoding='utf-8') as f1: f1.write('''每天坚持一点, 每天努力一点, 每天多思考一点, 慢慢你会发现, 你的进步越来越大。''')
from os import remove,rename with open('a1.txt','r',encoding='utf-8') as f1, open('a1.txt.tmp','a',encoding='utf-8') as f2: for i in f1.readlines(): new_line = i.replace('我说的都是真的。哈哈','你们就信吧~ 我说的都是真的。哈哈') f2.write(new_line) remove('a1.txt') rename('a1.txt.tmp','a1.txt')
8、有如下文件,t1.txt
葫芦娃,葫芦娃,
一根藤上七个瓜
风吹雨打,都不怕,
啦啦啦啦。
我可以算命,而且算的特别准:
分别完成下面的功能:
file = open('t1.txt','r+',encoding='utf-8') print(file.readable()) print(file.writable()) file.close()
with open('t1.txt','r',encoding='utf-8') as f1: for i in f1: print(i,end='')
with open('t1.txt','r',encoding='utf-8') as f1: for i in f1.readlines(): print(i,end='') # readlines与直接读取句柄内容或者read的区别在于。readlines以迭代的方式读取文件,读一行显示一行,而read会直接将整个文件缓存到内存中,再进行操作。
with open('t1.txt','r',encoding=',utf-8') as f1: print(f1.readline(4))
with open('t1.txt','r',encoding=',utf-8') as f1: print(f1.readline().strip())
f = open("t1.txt",mode='r',encoding="utf-8") for i in f: if i.startswith("风吹雨打"): print(i,end='') print(f.read()) f.flush() f.close()
f = open("t1.txt",mode='r+',encoding="utf-8") f.seek(24) f.truncate() f.seek(0) s = f.read() print(s) f.close()