第一套
6
fi = open("小女孩.txt", "r") fo = open("PY301-1.txt", "w") txt = fi.read() d = {} exclude = ", 。 ! ? 、 () 【】 《》 <> = : :+-*__“”..." for word in txt: if word in exclude: continue else: d[word] = d.get(word, 0) + 1 ls = list(d.items()) ls.sort(key=lambda x:x[1], reverse=True) fo.write("{}:{}".format(ls[0][0], ls[0][1])) fo.close() fi.close()
fi = open("小女孩.txt", "r") fo = open("PY301-2.txt", "w") txt = fi.read() d = {} for word in txt: d[word] = d.get(word, 0) + 1 del d["\n"] ls = list(d.items()) ls.sort(key=lambda x: x[1], reverse=True) for i in range(10): fo.write(ls[i][0]) fi.close() fo.close()
fi = open("小女孩.txt", "r") fo = open("小女孩-频次排序", "w") txt = fi.read() d = {} for word in txt: d[word] = d.get(word, 0) + 1 del d[" "] del d["\n"] ls = list(d.items()) ls.sort(key=lambda x: x[1], reverse=True) # 此行可以按照词频由高到低排序 fo.write(",".join(ls)) fo.close() fi.close()
第二套
6
fi = open("PY301-vacations.csv", "r") ls = [] for line in fi: ls.append(line.strip("\n").split(",")) s = input("请输入节假日名称:") for line in ls: if s == line[1]: print("{}的假期位于{}-{}之间".format(line[1], line[2], line[3])) fi.close()
fi = open("PY301-vacations.csv","r") ls = [] for line in fi: ls.append(line.strip("\n").split(",")) s = input("请输入节假日序号:").split(" ") while True: for i in s: for line in ls: if i == line[0]: print("{}({})假期是{}月{}日至{}月{}日之间".format((line[1]),(line[0]),line[2][:-2],line[2][-2:],line[3][:-2],line[3][-2:])) s = input("请输入节假日序号:").split(" ") fi.close()
fi = open("PY301-vacations.csv","r") ls = [] for line in fi: ls.append(line.strip("\n").split(",")) s = input("请输入节假日序号:").split(" ") while True: for i in s: flag = False for line in ls: if i == line[0]: print("{}({})假期是{}月{}日至{}月{}日之间".format((line[1]),(line[0]),line[2][:-2],line[2][-2:],line[3][:-2],line[3][-2:])) flag = True if flag == False: print("输入节假日编号有误!") s = input("请输入节假日序号:").split(" ") fi.close()
第三套
6
fi = open("论语.txt", "r") fo = open("论语-原文.txt", "w") flag = False for line in fi: if "【" in line: flag = False if "【原文】" in line: flag = True continue if flag == True: fo.write(line.lstrip()) fi.close() fo.close()
fi = open("论语-原文.txt", 'r') fo = open("论语-提纯原文.txt", 'w') for line in fi: for i in range(1,23): line = line.replace("({})".format(i),"") fo.write(line) fi.close() fo.close()
第四套
6
fi = open("sensor.txt", "r") fo = open("earpa001.txt", "w") txt = fi.readlines() for line in txt: ls = line.strip("\n").split(",") if " earpa001" in ls: fo.write('{},{},{},{}\n'.format(ls[0], ls[1], ls[2], ls[3])) fi.close() fo.close()
fi = open("earpa001.txt", "r") fo = open("earpa001_count.txt", "w") d = {} for line in fi: split_data = line.strip("\n").split(",") floor_and_area = split_data[-2] + "-" + split_data[-1] d[floor_and_area] = d.get(floor_and_area, 0) + 1 # if floor_and_area in d: # d[floor_and_area] += 1 # else: # d[floor_and_area] = 1 ls = list(d.items()) ls.sort(key=lambda x: x[1], reverse=True) # 该语句用于排序 for i in range(len(ls)): fo.write('{},{}\n'.format(ls[i][0], ls[i][1])) fi.close() fo.close()
第五套
6
fi = open("arrogant.txt","r") fo = open("PY301-1.txt","w") txt = fi.read() d = {} for s in txt: d[s] = d.get(s,0)+1 del d["\n"] ls =list(d.items()) for i in range(len(ls)): fo.write("{}:{}\n".format(ls[i][0],ls[i][1])) fo.close() fi.close()
fi = open("arrogant.txt","r") fo = open("arrogant-sort.txt","w") txt = fi.read() d = {} for s in txt: d[s] = d.get(s,0)+1 del d["\n"] ls =list(d.items()) ls.sort(key=lambda x:x[1],reverse=True) for i in range(10): fo.write("{}:{}\n".format(ls[i][0],ls[i][1])) fi.close() fo.close()
第六章
6
fi = open("score.csv", "r") fo = open("avg-score.txt", "w") ls = [] x = [] sum = 0 for row in fi: ls.append(row.strip("\n").split(",")) for line in ls[1:]: for i in line[1:]: sum = int(i) + sum avg = sum / 3 x.append(avg) sum = 0 fo.write("语文:{:.2f}\n数学:{:.2f}\n英语:{:.2f}\n物理:{:.2f}\n科学:{:.2f}".format(x[0], x[1], x[2], x[3], x[4])) fi.close() fo.close()
第七套
6
fo = open("PY301-1.txt","w") class Horse(): def __init__(self, category, gender, age): self.category = category self.gender = gender self.age = age def get_descriptive(self): self.info = "一匹" + self.category + str(self.age) + "岁的" + self.gender + "马" def write_speed(self, new_speed): self.speed = new_speed addr = "在草原上奔跑的速度为" fo.write(self.info + "," + addr + str(self.speed) + "km/h。") horse = Horse("阿拉伯","公",12) horse.get_descriptive() horse.write_speed(50) fo.close()
fo = open("PY301-2.txt","w") class Horse(): def __init__(self, category, gender, age): self.category = category self.gender = gender self.age = age def get_descriptive(self): self.info = "一匹" + self.category + str(self.age) + "岁的" + self.gender + "马" def write_speed(self, new_speed): self.speed = new_speed addr = "在草原上奔跑的速度为" fo.write(self.info + "," + addr + str(self.speed) + "km/h。") class Camel(Horse): def __init__(self, category, gender, age): super().__init__(category, gender, age) def write_speed(self,new_speed): self.speed = new_speed addr = "在沙漠上奔跑的速度为" fo.write(self.info.replace("马","骆驼") + "," + addr + str(self.speed) + "km/h。") camel = Camel("双峰驼","母",20) camel.get_descriptive() camel.write_speed(40) fo.close()
第八套
5
import math try: a = eval(input('请输入底数:')) b = eval(input('请输入真数:')) c = math.log(b, a) except ValueError: if a<=0 and b>0: print("底数不能小于等于0") elif b<=0 and a>0: print("真数不能小于等于0") elif a<=0 and b<=0: print('真数和低数都不能小于等于0') except ZeroDivisionError: print('底数不能为1') except NameError: print('输入必须为实数') else: print(c)
6
intxt = input("请输入明文:") for p in intxt: if "a" <= p <= "z": print(chr(ord("a") + (ord(p) - ord("a") + 3)%26), end="") elif "A" <= p <= "Z": print(chr(ord("A") + (ord(p) - ord("A") + 3)%26), end="") else: print(p,end="")
第九套
4
5
fo = open("PY202.txt","w") for i in range(1,10): for j in range(1,i+1): fo.write("{}*{}={}\t".format(j,i,i*j)) fo.write("\n") fo.close()
6
fi = open("关山月.txt","r") fo = open("关山月-诗歌.txt","w") for i in fi.read(): if i == "。": fo.write("。\n") else: fo.write(i) fi.close() fo.close()
fi = open("关山月-诗歌.txt","r") fo = open("关山月-反转.txt","w") txt = fi.readlines() txt.reverse() for line in txt: fo.write(line) fi.close() fo.close()
第十套
3
import time t = time.localtime() print(time.strftime("%Y年%m月%d日%H时%M分%S秒",t))
4
for i in range(0, 4): for y in range(0, 4 - i): print(" ", end="") print("* " * i) for i in range(0, 4): for x in range(0, i): print(" ", end="") print("* " * (4 - i))
第十一套
1
f = open("poem.txt","r") result = [] for line in f.readlines(): line = line.strip("\n") if len(line) != 0 and line[0] != "#": result.append(line) result.sort() for line in result: print(line) f.close()
5
def proc(stu_list): d = {} for item in stu_list: r = item.split("_") a,b = r[0],r[1].strip() if a in d: d[a] += [b] else: d[a] = [b] lst = sorted(d.items(), key = lambda d:len(d[1]), reverse = True) return lst f = open("signup.txt","r") stu_list = f.readlines() result = proc(stu_list) for item in result: print(item[0], '->', item[1]) f.close()
6
d = {"lili": 80, "xiaoqiang": 75, "yunyun": 89, "yuanyuan": 90, "wanghao": 85} d_sort = sorted(d.items(), key=lambda x: x[1], reverse=True) for i in range(3): print(d_sort[i][0] + " " + str(d_sort[i][1]))