實驗2字串和列表
阿新 • • 發佈:2022-04-10
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))
實驗1總結:
print(f'{i:02d}', end = '-') # 指定每個整數輸出寬度佔兩列,不足兩列,左邊補0
[str(i) for i in range(10)] # 函式str()用於把其它型別物件轉換成字串物件
[str(i).zfill(2) for i in range(10)] # 方法.zfill()用於對字串進行格式化,
指定寬度為2列,不足左邊補0
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))
實驗2 總結:對於列表中首字母大寫:name.title()
name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins'] a1=sorted(name_list)for name in a1: print(name.title())
實驗3 總結:字典排序sorted()
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. Now is better than never. 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!''' linenum=0 letternum=0 strinum=0 spacenum=0 linenum=text.count('\n')+1 print(f'行數是:{linenum}') for i in text: if i==' ': spacenum+=1 print(f'單詞數是:{len(text.split())}') print(f'字元數是:{len(text)}') print(f'空格數是:{spacenum}')
實驗4 總結:統計行數,字元數,單詞數的方法
book_list = [['靜靜的頓河','肖洛霍夫','金人', '人民文學出版社'], ['大地之上','羅欣頓.米斯特里','張亦琦', '天地出版社'], ['夜航西飛', '柏瑞爾.馬卡姆', '陶立夏', '人民文學出版社'], ['來自民間的叛逆', '袁越', '','新星出版社'], ['科技與惡的距離', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'], ['燈塔','克里斯多夫.夏布特','呂俊君','北京聯合出版公司'], ['小行星掉在下午','沈大成', '', '廣西師範大學出版社']] for index,book in enumerate(book_list): print(f'{index+1}.《{book[0]}》|{book[1]}|{book[3]}')
實驗5總結:靈活使用index函式來排序即對應的是for index,x in enumerate()
"""99 81 75 30 42 90 87 69 50 96 77 89, 93 82, 99, 78, 100 """ data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100'] c=0 n=0 for i in data: for b in i.split(): c=c+int(b) n+=1 a=c/n print('{:.2f}'.format(a))
實驗6總結:回憶了關於保留小數、精度的問題
words_sensitive_list = ['張三', 'V字仇殺隊', '殺'] comments_list = ['張三因生命受到威脅正當防衛導致過失殺人,經辯護律師努力,張三不需負刑事責任。','電影<V字仇殺隊>從豆瓣下架了','娛樂至死'] for i in comments_list: n = 0 for x in words_sensitive_list: n+=1 if x in i: i=i.replace(x,'*'*len(x)) else: i=i if n==3: print(i)
實驗7總結:學會了用以替代關鍵字的方法