其他分享
首页 > 其他分享> > 实验五

实验五

作者:互联网

Task3:

 1 with open('实验5数据文件/data3.txt', 'r') as f:
 2     data = f.readlines()
 3 
 4 data[0] = data[0].rstrip('\n')
 5 
 6 for i in range(len(data) - 1):
 7     data[i + 1] = eval(data[i + 1].rstrip('\n'))
 8 print(f'{data[0]}:')
 9 print(data[1:])
10 
11 data1 = []
12 for i in data[1:]:
13     data1.append(round(i))
14 
15 data1.insert(0, '四舍五入后数据')
16 
17 print(data1[0])
18 print(data1[1:])
19 
20 data3 = []
21 for i in range(len(data)):
22     data3.append(str(data[i]) +'\t' * 2 + str(data1[i]) + '\n')
23 
24 with open('实验5数据文件/data3_processed.txt', 'w') as f:
25     f.writelines(data3)

 

 

Task4:

 1 with open('C://Users//sanya//Desktop//实验五//实验5数据文件//data4.txt', 'r', encoding = 'utf-8') as f:
 2     data = f.readlines()
 3 
 4 for i in range(len(data)):
 5     data[i] = data[i].rstrip('\n')
 6 
 7 data1 = []
 8 for i in range(len(data)):
 9     data1.append(data[i].split('\t'))
10 
11 def xulie(elem):
12     return elem[2]
13 
14 def xulie1(elem):
15     return elem[3]
16 
17 x = data1.pop(0)
18 
19 data1.sort(key = xulie)
20 
21 zhuanye = set()
22 for i in range(len(data1)):
23     zhuanye.add(data1[i][2])
24 zhuanye = sorted(list(zhuanye))
25 
26 data3 = []
27 for i in zhuanye:
28     data2 = []
29     for n in range(len(data1)):
30         if(data1[n][2] == i):
31             data2.append(data1[n])
32     data2.sort(key = xulie1, reverse=True)
33     for m in data2:
34         data3.append(m)
35 
36 data3.insert(0, x)
37 for i in range(len(data3)):
38     print('{}\t{}\t{}\t{}'.format(data3[i][0], data3[i][1], data3[i][2], data3[i][3]))
39 
40 for i in range(len(data3)):
41     data3[i] = '\t'.join(data3[i])
42     data3[i] = data3[i] + '\n'
43 
44 with open('C://Users//sanya//Desktop//实验五//实验5数据文件//data4_processed.txt', 'w') as f:
45     f.writelines(data3)

 

 

 

 Task6:

 1 with open('实验5数据文件/data5.txt', 'r', encoding = 'utf-8')as f:
 2     data = f.readlines()
 3 
 4 hangshu = len(data)
 5 data2 = data.copy()
 6 for i in data:
 7     if(i == '\n'):
 8         data.remove('\n')
 9 
10 for i in range(len(data)):
11     data[i] = data[i].rstrip('\n')
12 
13 data1 = data.copy()
14 for i in range(len(data)):
15     data1[i] = data1[i].split(' ')
16 sum = 0
17 for i in data1:
18     sum = len(i) + sum
19 
20 zishu = sum
21 sum = 0
22 for i in data:
23     sum = i.count(' ') + sum
24 kongge = sum
25 zifu = len(''.join(data2))
26 print('行数:{}\n单词数:{}\n空格数:{}\n字符数:{}'.format(hangshu, zishu, kongge, zifu))
27 
28 with open('实验5数据文件/data5_with_line.txt', 'w', encoding='utf-8')as f:
29     for i in range(hangshu):
30         f.writelines(str(i + 1) + ' ' + data2[i])

 

标签:data1,sum,len,range,实验,data,data3
来源: https://www.cnblogs.com/Cr2O3/p/16259532.html