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

实验2

作者:互联网

task1

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

task2

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

task3

 

1 name_list=['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
2 i=1
3 name_list=sorted(name_list)
4 for name in name_list:
5     print(i,'.',name.title())
6     i=i+1

 

task4

 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 line=text.split('\n')
23 word=text.split(' ')
24 print('行数:',len(line))
25 print('单词数:',len(word))
26 print('字符数:',len(text))
27 print('空格数:',text.count(' '))

 

task5

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

 

task6

 1 data=['99 81 75','30 42 90 87','69 50 96 77 89 93','82 99 78 100']
 2 num=0
 3 n=0
 4 for i in data:
 5     s=i.split()
 6     for j in s:
 7         num+=eval(j)
 8     n+=len(s)
 9 a=num/n
10 print('{:.2f}'.format(a))

 

task7

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

 

 

实验总结:

 (1)通过这次实验,我更加熟练的掌握了字符串和列表的表示,以及索引、切片等各种操作,对之前的知识进行了复习,同时也补充了许多之前学习上的不足。

(2)在本次实验中,也发现了我之前存在的潜藏问题在于输出和格式控制方面有许多不熟练的地方,很多这方面操作需要用到的函数等不了解或者运用不熟练,在之后的学习中要多加练习,补充这方面的不足。

标签:10,name,list,better,实验,print,than
来源: https://www.cnblogs.com/dyh222/p/16110236.html