首页 > 其他分享 >实验5

实验5

时间:2023-06-05 23:11:39浏览次数:39  
标签:lst1 data len 实验 print data1 append

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')

运行截图:

 

 

 

task7

源代码:

 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]))

运行截图:

 

 

 

 

 

 task8.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)

运行截图:

 

 

 

 

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])

运行截图:

 

 

 

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)

运行截图:

 

总结: 1.datetime模块中需要注意大小写。   如:t.strftime('%Y%m%d') 结果为20230605     而t.strftime('%y%m%d')结果为230605  2.任务7对多个关键字排序的代码感觉还是有一些人为排序的因素,需要改进,但目前没有想到更好的方法。    

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

相关文章

  • 实验6 turtle绘图与python库应用编程体验
    实验任务一task1_1fromturtleimport*defmove(x,y):penup()goto(x,y)pendown()defdraw(n,size=100):foriinrange(n):fd(size)left(360/n)defmain():pensize(2)pencolor('red')move(-200,0)......
  • 实验6
    1、实验任务1task1_1.py实验源码:1fromturtleimport*23defmove(x,y):4penup()5goto(x,y)6pendown()78defdraw(n,size=100):9foriinrange(n):10fd(size)11left(360/n)1213defmain():14pens......
  • 实验5 文件应用编程
    task6.py1withopen('data6.csv','r',encoding='gbk')asf:2data=list(f.readlines())3data=[i.strip('\n')foriindata]45importdecimal6decimal.getcontext().rounding='ROUND_HALF_UP'7......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1:使用turtle绘制基础图形task1_1.py实验源码:1fromturtleimport*23defmove(x,y):4penup()5goto(x,y)6pendown()78defdraw(n,size=100):9foriinrange(n):10fd(size)11left(360/n)12......
  • 实验5
    6.实验任务6task6.py实验源码:1withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]4processed_data=[]5foriinrange(len(old_data)):6ifeval(old_dat......
  • 实验5 文件应用编程
    实验任务6实验源码:1withopen('data6.csv','r',encoding='gbk')asf:2raw_data=f.read().split('\n')3title1=['原始数据']45data1=[float(i)foriinraw_data[1:]]6print(f'{title1[0]......
  • Python实验课5
    实验任务六实验源码:1withopen('data6.csv','r',encoding='gbk')asf:2data=f.readlines()3data1=[str(eval(data[i]))foriinrange(1,len(data))]4data2=[str(int(eval(data[i])+0.5))foriinrange(1,len(data))]5info=......
  • 实验五
    实验任务6task6:1withopen('data6.csv','r',encoding='gbk')asf:2data=list(f.readlines())3data=[i.strip('\n')foriindata]45importdecimal6decimal.getcontext().rounding='ROUND_HALF_UP'......
  • 实验五
    withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()processed_data=[]foriinrange(1,len(data)):row=data[i].strip()processed_item=round(float(row))processed_data.append(proces......
  • 实验5
    6.实验任务6程序源代码:withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()data_title=['原始数据','四舍五入后数据']data.remove('原始数据\n')data_zahl=[]foriindata:ifi[-1]==�......