实验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',
2 'cocteau twins']
3 
4 for name in name_list:
5     print(name.title())
6 print()
7 
8 name_list_captilize = [name.title() for name in name_list]
9 print('\n'.join(name_list_captilize))
复制代码

 

 

 

 

Task3:

 

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

 

 

 

Task4:

复制代码
 1 this = '''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 this_list = this.split()
23 this_list1 = this.splitlines()
24 hangshu = len(this_list1)
25 zifu = len(this)
26 danci = len(this_list)
27 kongge = this.count(' ')
28 print('行数:{}'.format(hangshu))
29 print('单词数:{}'.format(danci))
30 print('字符数:{}'.format(zifu))
31 print('空格数:{}'.format(kongge))
复制代码

 

 

Task5:

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

 

 

 

总结:.pop()函数,删除数列最后一个元素(可被接收),空数列会报cuo

Task6:

 

复制代码
 1 average = []
 2 data = ['99 81 75','30 42 90 87','69 50 96 77 89 93','82 99 78 100']
 3 for i in range(len(data)):
 4     data[i] = data[i].split(' ')
 5 for i in range(len(data)):
 6     sum = 0
 7     for n in range(len(data[i])):
 8         sum = sum + eval(data[i][n])
 9     average.append(sum)
10 changdu = 0
11 for i in range(len(data)):
12     changdu = changdu + len(data[i])
13 sum = 0
14 for i in range(len(average)):
15     sum = sum + average[i]
16 average_end = sum / changdu
17 print('{:.2f}'.format(average_end))
复制代码

 

 

Task7:

 

复制代码
words_sensitive_list = ['张三', 'V字仇杀队', '杀']
comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。',
'电影<V字仇杀队>从豆瓣下架了',
'娱乐至死']
for i in range(len(comments_list)):
    for n in range(len(words_sensitive_list)):
        if(words_sensitive_list[n] in comments_list[i]):
            comments_list[i] = comments_list[i].replace(words_sensitive_list[n],'*'*int(len(words_sensitive_list[n])))
for i in comments_list:
    print(i)
复制代码

 

 

总结:

1.str.title()的用法:所有单词都以大写开始,其余字母均为小写,做成标题的形式。

2.enumerate()的用法:用于将一个可遍历的数据对象组合为一个索引序列,将数据和数据下标联系起来。

3.str.replace()的用法:把字符串中的旧字串替换成新子串,括号内前面是被替换的,后面是替换元素。

 

posted @ 2022-04-07 14:23  WA自动机!  阅读(24)  评论(3编辑  收藏  举报