实验二。付子安202183820035
1
x = list(range(10)) print('整数输出1: ', end = '') for i in x: print(i, end=' ') print('\n整数输出2: ', end = '') for i in x: print(f'{i:02d}', end = '-') print('\n整数输出3: ', end = '') for i in x[:-1]: print(f'{i:02d}', end = '-') print(f'{x[-1]:02d}') print('\n字符输出1: ', end = '') y1 = [str(i)for i in range(10)] print('-'.join(y1)) print('字符输出2: ', end = '') y2 = [str(i).zfill(2) for i in range(10)] print('-'.join(y2))

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

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

4
text = """The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!""" text1=text.split('\n') text2=text.split(' ') print('行数:',len(text1)) print('单词数:',len(text2)) print('字符数:',len(text)) print('空格数',text.count(' '))

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

6
data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100'] c=0 i=0 for list1 in data: a=list1.split(' ') b=0 for x in a: b=b+int(x) i=i+1 c=c+b d=c/i print('%.2f'%d)

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

实验总结
关于这次的实验,我对python中的各种代码又加深了一遍印象,其中对字符串,列表的区别有了更深刻的理解。
本次实验让我对于平时不习惯使用的函数如sorted title split的用法更加熟悉,对于字符串中的替换操作加深了印象
关于这次实验我对于for in 指令的实际作用判断失误,导致代码混乱,耽误了大量时间,需要吸取教训,加强这方面的认识

浙公网安备 33010602011771号