实验2 字符串和列表

 实验任务1:
1
x=list(range(10)) 2 print('整数输出1:',end=' ') 3 for i in x: 4 print(i,end=' ') 5 print('\n整数输出2:',end=' ') 6 for i in x: 7 print(f'{i:02d}',end='-') 8 print('\n整数输出3:',end=' ') 9 for i in x[:-1]: 10 print(f'{i:02d}',end='-') 11 print(f'{x[-1]:02d}') 12 print('\n字符输出1:',end=' ') 13 y1=[str(i) for i in range(10)] 14 print('-'.join(y1)) 15 print('字符输出2:',end=' ') 16 y2=[str(i).zfill(2) for i in range(10)] 17 print('-'.join(y2))

 

实验任务2:
1
name_list=['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins'] 2 for name in name_list: 3 print(name.title()) 4 print() 5 6 name_list_captilize=[name.title() for name in name_list] 7 print('\n'.join(name_list_captilize))

  

实验任务3:
1
name_list=['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins'] 2 name_list2=sorted(name_list) 3 for name in name_list2: 4 print(f'{name_list2.index(name)+1}.',name.title())

 

 

实验任务4:
1
text='''The Zen of Python, by Tim Peters 2 3 Beautiful is better than ugly. 4 Explicit is better than implicit. 5 Simple is better than complex. 6 Complex is better than complicated. 7 Flat is better than nested. 8 Sparse is better than dense. 9 Readability counts. 10 Special cases aren't special enough to break the rules. 11 Although practicality beats purity. 12 Errors should never pass silently. 13 Unless explicitly silenced. 14 In the face of ambiguity, refuse the temptation to guess. 15 There should be one-- and preferably only one --obvious way to do it. 16 Although that way may not be obvious at first unless you're Dutch. 17 Now is better than never. 18 Although never is often better than *right* now. 19 If the implementation is hard to explain, it's a bad idea. 20 If the implementation is easy to explain, it may be a good idea. 21 Namespaces are one honking great idea -- let's do more of those!''' 22 hang=len(text.splitlines()) 23 zifu=len(text) 24 words=len(text.split()) 25 kongs=0 26 for i in text: 27 if i ==" ": 28 kongs+=1 29 print('行数:',hang) 30 print('单词数:',words) 31 print('字符数:',zifu) 32 print('空格数:',kongs)

 

实验任务5:
1
book_list=[['静静的顿河','肖洛霍夫','金人', '人民文学出版社'], 2 ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'], 3 ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'], 4 ['来自民间的叛逆', '袁越', ' ','新星出版社'], 5 ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'], 6 ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'], 7 ['小行星掉在下午','沈大成', ' ', '广西师范大学出版社']] 8 for i in book_list: 9 print(f'{book_list.index(i)+1}.','',i[0],'','|',i[1],'|',i[3])

 

实验任务6:
1
date = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100'] 2 s=0 3 number=0 4 for i in date: 5 b=i.split() 6 for j in b: 7 s=s+int(j) 8 number+=1 9 print('{:.2f}'.format(s/number))

 


实验任务7:
1
words_sensitive_list = ['张三', 'V字仇杀队', ''] 2 comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。', 3 '电影<V字仇杀队>从豆瓣下架了', 4 '娱乐至死'] 5 a='\n'.join(comments_list) 6 b=a.replace("张三","**") 7 c=b.replace("","*") 8 d=c.replace("V字仇*队","*****") 9 print(d)

 

实验总结:

1、通过本次实验知道了如何取两位小数、如何将列表里的元素换行以及如何将元素按字典序编号输出;

2、虽然结果运行答案一致,但是做题的过程仍需要翻阅资料,不熟练。

posted @ 2022-04-07 21:00  暮渎  阅读(30)  评论(1编辑  收藏  举报