首页 > 其他分享 >实验5

实验5

时间:2023-06-06 20:46:16浏览次数:36  
标签:lst1 data len 实验 print data1 append

实验任务6

task6.py

 1 with open('data6.csv','r',encoding='gbk') as f:
 2     data1 = f.read().split('\n')
 3 del data1[0]
 4 print('原始数据:')
 5 print(data1)
 6 
 7 for i in range(len(data1)):
 8     data1[i] = float(data1[i])
 9 data2 = []
10 for i in range(len(data1)):
11     data2.append(int(data1[i]+0.5))
12 print('四舍五入后数据:')
13 print(data2)
14 
15 title = ['原始数据','四舍五入后数据']
16 info = []
17 for i in range(len(data1)):
18     info.append([str(data1[i]),str(data2[i])])
19 with open('data6_processed.csv','w',encoding='gbk') as f:
20     f.write(','.join(title)+'\n')
21     for item in info:
22         f.write(','.join(item)+'\n')

实验任务7

task7.py

 1 with open('data7.csv','r',encoding='gbk') as f:
 2     data1 = f.read().split('\n')
 3 del data1[0]
 4 
 5 lsta = []
 6 lstm = []
 7 for i in data1:
 8     data1.sort(key = lambda i :(i[-2],i[-1]),reverse = True)
 9 for i in data1:
10     lst1 = i.split(',')
11     if lst1[2] == 'Acting':
12         lsta.append(lst1)
13     else:
14         lstm.append(lst1)
15 
16 info = lsta+lstm
17 title = ['学号','姓名','专业','分数']
18 with open('data7_processed.csv','w',encoding='gbk') as f:
19     f.write(','.join(title)+'\n')
20     for items in info:
21         f.write(','.join(items)+'\n')
22 print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3]))
23 for i in info:
24     print('{:<10}'.format(i[0]),'{:<10}'.format(i[1]),'{:<10}'.format(i[2]),'{:<15}'.format(i[3]))

实验任务8

task.py

 1 with open('hamlet.txt','r') as f:
 2     data = f.read()
 3 print('hamlet.txt粗略统计:')
 4 data1 = data.splitlines()
 5 print(f'行数:{len(data1)}')
 6 print(f'单词数:{len(data.split())}')
 7 print(f'字符数:{len(data)}')
 8 k = data.count(' ')
 9 print(f'空格数:{k}')
10 
11 data2=[]
12 for i in range(len(data1)):
13     data2.append(f'{i+1} '+ data1[i] + '\n')14
15 with open('hamlet_with_line_number.txt','w') as f:
16     f.writelines(data2)

实验任务9

task9.py

 1 def is_valid(a):
 2     if len(a) != 18:
 3         return False
 4     elif (a[:-1].isnumeric() and a[-1] == 'X') or (a.isnumeric()):
 5         return True
 6     else:
 7         return False
 8 
 9 with open('data9_id.txt','r') as f:
10     data = f.read().split('\n')
11 del data[0]
12 data1 = []
13 print('姓名,出生日期,年龄')
14 for i in data:
15     lst = i.split(',')
16     data1.append(lst)
17 lst1 = []
18 import datetime
19 t = datetime.datetime.now().year
20 for i in data1:
21     if is_valid(i[1]) == True:
22         name = i[0]
23         btd = i[1][6:14]
24         age = str(int(t)-int(i[1][6:10]))
25         lst1.append([name,btd,age])
26 lst1.sort(key=lambda x:x[2],reverse=True)
27 for i in lst1:
28     print(i[0],end=',')
29     print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}',end=',')
30     print(i[2])

实验任务10

task10_1.py

 1 with open('data10_stu.txt','r',encoding='utf-8') as f:
 2     data = f.readlines()
 3 print('{:*^40}'.format('抽点开始'))
 4 n = int(input('输入随机抽点人数:'))
 5 import random
 6 x = []
 7 while len(set(x)) < n:
 8     x.append(random.randint(0, len(data)-1))
 9 
10 lst = []
11 for i in x:
12     print(data[i],end = '\n')
13     lst.append(data[i])
14 
15 import datetime
16 t = datetime.datetime.now()
17 y = t.strftime('%Y%m%d')
18 with open(f'{y}.txt','w',encoding='utf-8') as f:
19     f.writelines(lst)

task10_2.py

 1 with open('data10_stu.txt','r',encoding='utf-8') as f:
 2     data = f.readlines()
 3 print('{:*^40}'.format('抽点开始'))
 4 lst1 = []
 5 x = []
 6 while True:
 7     n = int(input('输入随机抽点人数:'))
 8     if n == 0:
 9         print('{:*^40}'.format('抽点结束'))
10         break
11     else:
12         x = []
13         import random
14         while len(x) < n:
15             t = random.randint(0, len(data)-1)
16             if (str(t) not in lst1) and (str(t) not in x):
17                 x.append(t)
18                 lst1.append(t)
19 
20         lst = []
21         for i in x:
22             print(data[i],end = '\n')
23         lst.append(data[i])
24 
25         import datetime
26         t = datetime.datetime.now()
27         y = t.strftime('%Y%m%d')
28         with open(f'{y}.txt','w',encoding='utf-8') as f:
29             f.writelines(lst)

 

标签:lst1,data,len,实验,print,data1,append
From: https://www.cnblogs.com/wang2003chengxu/p/17461666.html

相关文章

  • 实验5 文件应用编程
    实验任务6task6实验源码1withopen('data6.csv','r',encoding='gbk')asf:2data1=f.read().split('\n')3deldata1[0]4print('原始数据:')5print(data1)67foriinrange(len(data1)):8data1[i......
  • 实验5
    6.实验任务6task6.py实验代码:withopen('data6.csv','r',encoding='gbk')asf:data1=f.read().split('\n')deldata1[0]print('原始数据:')print(data1)foriinrange(len(data1)):data1[i]=float(data1[i]......
  • 实验5
    task6withopen('data6.csv','r',encoding='gbk')asf:data=list(f.readlines())data=[i.strip('\n')foriindata]importdecimaldecimal.getcontext().rounding='ROUND_HALF_UP'title=[data[......
  • 实验5 文件应用编程
    task6源代码 withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()data.pop(0)data=[eval(i.strip())foriindata]print(f'原始数据:\n{data}')processed_data=[int(i+0.5)foriindata]......
  • 实验5 文件应用编程
    实验5文件应用编程实验任务6: task6.py1withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]4processed_data=[]5foriinrange(len(old_data)):6ifeval(ol......
  • 实验5 文件应用编程
    task6源代码:withopen('data6.csv','r',encoding='gbk')asf:old_data=f.read().split('\n')delold_data[0]new_data=[]foriinrange(len(old_data)):ifeval(old_data[i])+float(0.5)>=int(eval(o......
  • 实验五
    task6源代码:withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()data.remove('原始数据\n')data1=[]foriindata:ifi[-1]=='\n':i=i[:-1]data1.append(float(i))print(&#......
  • 实验5 文件应用编程
    实验任务61withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]4processed_data=[]5foriinrange(len(old_data)):6ifeval(old_data[i])+0.5>=......
  • 实验5
    task6withopen('data6.csv','r',encoding='gbk')asf:old_data=f.read().split('\n')delold_data[0]processed_data=[]foriinrange(len(old_data)):ifeval(old_data[i])+0.5>=int(eval(old_data......
  • 实验5
    task61withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]45new_data=[]6foriinrange(len(old_data)):7ifeval(old_data[i])+float(0.5)......