实验二

task1

 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 
16 print('\n字符输出2: ',end='')
17 y2=[str(i).zfill(2) for i in range(10)]
18 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 print()
5 name_list_caption = [name.title() for name in name_list]
6 print('\n'.join(name_list_caption))

task3

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

task4

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

task5

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

task6

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

 task7

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

 

posted @ 2022-04-11 16:21  雷建彬  阅读(30)  评论(3编辑  收藏  举报