实验二

 1 # 基础操作练习: 列表及列表推导式、格式化、类型转换
 2 x = list(range(10))
 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))

 1 # 把姓名转换成大写,遍历输出
 2 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan','cocteau twins']
 3 
 4 # 实现方式1
 5 for name in name_list:
 6     print(name.title())
 7 
 8 print()
 9 
10 # 实现方式2
11 name_list_captilize = [name.title() for name in name_list]
12 print('\n'.join(name_list_captilize))

1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan','cocteau twins']
2 for i in name_list:
3     print(str(name_list.index(i))+'.'+i.capitalize())

 1 z=0
 2 text='''The Zen of Python, by Tim Peters
 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 '''
23 print('空格数:',len(text.split()))
24 for i in text:
25     z+=1
26 print('单词数:',z)
27 print('行数:',str(text.count('\n')+1))
28 print('字符数:', text.count(' '))

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

 1 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
 2 b=[]
 3 x=0
 4 y=0
 5 for i in data:
 6     a=i.split()
 7     b=b+a
 8 for j in b :
 9     x=x+int(j)
10     y+=1
11 print(round(x/y,2))

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

 

posted @ 2022-04-11 17:02  pray4hell  阅读(9)  评论(3编辑  收藏  举报