實驗2 字符串和列表
實驗二:
實驗任務一:
task1:
1 1 # 字符串的基礎操作 2 2 3 3 x = 'nba FIFA' 4 4 print(x.upper()) 5 5 print(x.lower()) 6 6 print(x.swapcase()) # 字符串大小寫翻轉 7 7 print() 8 8 9 9 x = 'abc' 10 10 print(x.center(10, '*')) # 字符串居中, 寬度10列,不足左右補*號 11 11 print(x.ljust(10, '*')) # 字符串居左, 寬度10列,不足右補*號 12 12 print(x.rjust(10, '*')) # 字符串居右, 寬度10列,不足左補*號 13 13 print() 14 14 15 15 x = '123' 16 16 print(x.zfill(10)) # 字符串寬度10列,不足左邊0補足 17 17 x = 123 18 18 print(str(x).zfill(10)) # int轉換為字符串,對字符串對象使用str。zfill() 19 19 print() 20 20 21 21 x = 'phone_number' 22 22 print(x.isidentifier()) # 判斷字符串是否是python合法標識符 x = '222test' p 23 23 x = '222test' 24 24 print(x.isidentifier()) 25 25 print() 26 26 27 27 x = ' ' 28 28 print(x.isspace()) # 判斷字符串是否是空白符(包括空格、回車、Tab鍵) 29 29 x = '\n' 30 30 print(x.isspace()) 31 31 print() 32 32 33 33 x = 'python is fun' 34 34 table = x.maketrans('thon', '1234') # 為字符串對象x創建一個字符映射表, 字符thon分別映射到字符 35 35 print(x.translate(table)) # 根據字符映射表table對字符串對象x中的字符進行轉換
運行結果:

實驗任務二:
task2:
1 1 # 基礎練習: 列表、格式化、類型轉換 2 2 3 3 x = [5, 11, 9, 7, 42] 4 4 5 5 print('整數輸出1: ',end = '') 6 6 i = 0 7 7 while i < len(x): 8 8 print(x[i], end = ' ') 9 9 i += 1 10 10 11 11 print('\n整數輸出2:', end = '') 12 12 i = 0 13 13 while i<len(x): 14 14 print(f'{x[i]:02d}', end = '-') # 指定每個整數寬度占2列;不足2列,左邊補0 i 15 15 i += 1 16 16 17 17 print('\n整數輸出3: ', end = '') 18 18 i = 0 19 19 while i<len(x): 20 20 print(f'{x[i]:02d}', end = '-') 21 21 i += 1 22 22 print(f'{x[-1]:02d}') 23 23 24 24 print('\n字符輸出1:', end = '') 25 25 y1 = [] 26 26 i = 0 27 27 while i<len(x): 28 28 y1.append(str(x[i])) # 函數str()用于把其它類型對象轉換成字符串對象 29 29 i += 1 30 30 print('-'.join(y1)) 31 31 32 32 print('字符輸出2:', end = '') 33 33 y2 = [] 34 34 i = 0 35 35 while i<len(x): 36 36 y2.append(str(x[i]).zfill(2)) # x[i]是int類型對象,使用str()轉換成字符串對象后,處理,然后加入到列表y中 37 37 i += 1 38 38 print('-'.join(y2))
運行結果:

line3更換后:

實驗任務三:
task3:
1 1 # 把姓名轉換成大寫,遍歷分行輸出 2 2 3 3 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins'] 4 4 5 5 # 方法1 6 6 i = 0 7 7 while i<len(name_list): 8 8 i+=1 9 9 10 10 print() 11 11 12 12 # 方法2 13 13 t = [] 14 14 i = 0 15 15 while i<len(name_list): 16 16 t.append(name_list[i].title()) 17 17 i += 1 18 18 19 19 print('\n'.join(t))
運行結果:

實驗任務四:
task4:
1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan','cocteautwins'] 2 change = [i.title() for i in name_list] 3 for i, item in enumerate(sorted(change)): 4 print((i+1),'.', item)
運行結果:

實驗結論:
推論式運用可以簡寫多行代碼,且推論式不僅僅可以用于列表,同時可以用于字符串。
實驗任務五:
task5:
1 x = """ 2 The Zen of Python, by Tim Peters 3 4 Beautiful is better than ugly. 5 Explicit is better than implicit. 6 Simple is better than complex. 7 Complex is better than complicated. 8 Flat is better than nested. 9 Sparse is better than dense. 10 Readability counts. 11 Special cases aren't special enough to break the rules. 12 Although practicality beats purity. 13 Errors should never pass silently. 14 Unless explicitly silenced. 15 In the face of ambiguity, refuse the temptation to guess. 16 There should be one-- and preferably only one --obvious way to do it. 17 Although that way may not be obvious at first unless you're Dutch. 18 Now is better than never. 19 Although never is often better than *right* now. 20 If the implementation is hard to explain, it's a bad idea. 21 If the implementation is easy to explain, it may be a good idea. 22 Namespaces are one honking great idea -- let's do more of those!""" 23 print('行數:',x.count('\n')) 24 25 y = x.split() 26 word = len(y) 27 print('單詞數:',word) 28 29 string = len(x) 30 print('字符數:',string) 31 32 print('空格數:',x.count(' '))
運行結果:

實驗結論:
對字符串的單詞數據統計,對行數統計,對空格數統計,有多種方法。但是我的實驗代碼在行數和字符數上一直沒有解決問題所在。‘'''’如果換行去掉,字符數就對了,但行數就不對了。
實驗任務六:
task6:
1 book_list = [['靜靜的頓河','肖洛霍夫','金人', '人民文學出版社'], 2 ['大地之上','羅欣頓.米斯特里','張亦琦', '天地出版社'], 3 ['夜航西飛', '柏瑞爾.馬卡姆', '陶立夏', '人民文學出版社'], 4 ['來自民間的叛逆', '袁越', '','新星出版社'], 5 ['科技與惡的距離', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'], 6 ['燈塔','克里斯多夫.夏布特','呂俊君','北京聯合出版公司'], 7 ['小行星掉在下午','沈大成', '', '廣西師范大學出版社']] 8 center = '圖書信息' 9 print(f'{center:*^40}') 10 i = 0 11 while i < len(book_list): 12 '|'.join(book_list[i]) 13 print(i+1,'.','|'.join(book_list[i])) 14 i += 1
運行結果:

實驗任務七:
task7:
1 ''' 2 某.csv格式數據文件內數據如下: 3 99 81 75 4 30 42 90 87 5 69 50 96 77 89, 93 6 82, 99, 78, 100 7 ''' 8 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100'] 9 a = str(data[0]) 10 a_list = a.split() 11 b = str(data[1]) 12 b_list = b.split() 13 c = str(data[2]) 14 c_list = c.split() 15 d = str(data[3]) 16 d_list = d.split() 17 datas = [int(i) for i in a_list] 18 datass = [int(i) for i in b_list] 19 datasss = [int(i) for i in c_list] 20 datassss = [int(i) for i in d_list] 21 n = 0 22 while n < len(datass): 23 datas.append(datass[n]) 24 n += 1 25 n = 0 26 while n < len(datasss): 27 datas.append(datasss[n]) 28 n += 1 29 n = 0 30 while n < len(datassss): 31 datas.append(datassss[n]) 32 n += 1 33 avg = sum(datas)/len(datas) 34 print(f'{avg:.2f}')
運行結果:

實驗任務八:
task8:
1 words_sensitive_list = ['張三', 'V字仇殺隊', '殺'] 2 comments_list = ['張三因生命受到威脅正當防衛導致過失殺人,經辯護律師努力,張三不需負刑事責任。', 3 '電影<V字仇殺隊>從豆瓣下架了', 4 '娛樂至死'] 5 wsl = "".join(map(str, words_sensitive_list)) 6 cl = "" 7 for i in comments_list: 8 cl += str(i)+" " 9 for i in wsl: 10 fw = i.strip() 11 if fw in cl: 12 cl = cl.replace(fw, '*'*len(fw)) 13 else: 14 lcl = cl.split() 15 i = 0 16 while i < len(lcl): 17 print(lcl[i]) 18 i += 1
運行結果:

實驗任務九:
task9:
示例源代碼:
1 """ 2 家用電器銷售系統 3 v1.0 4 """ 5 6 # 歡迎信息 7 print('歡迎使用家用電器銷售系統!') 8 9 # 產品信息列表 10 print('產品和價格信息如下:') 11 print('*'*60) 12 print('%-10s'%'編號','%-10s'%'名稱','%-10s'%'品牌','%-10s'%'價格','%-10s'%'庫存數量') 13 print('-'*60) 14 print('%-10s'%'0001','%-10s' %'電視機','%-10s' %'海爾', '%10.2f' %5999.00, '%10d' %20) 15 print('%-10s'%'0002','%-10s' %'冰箱', '%-10s' %'西門子','%10.2f' %6998.00, '%10d' %15) 16 print('%-10s'%'0003','%-10s' %'洗衣機','%-10s' %'小天鵝', '%10.2f' %1999.00, '%10d' %10) 17 print('%-10s'%'0004','%-10s' %'空調', '%-10s' %'格力', '%10.2f' %3900.00, '%10d' %0) 18 print('%-10s'%'0005','%-10s' %'熱水器','%-10s' %'美的', '%10.2f' %688.00, '%10d' %30) 19 print('%-10s'%'0006','%-10s' %'筆記本','%-10s' %'聯想', '%10.2f' %5699.00, '%10d' %10) 20 print('%-10s'%'0007','%-10s' %'微波爐','%-10s' %'蘇泊爾','%10.2f' %480.50, '%10d' %33) 21 print('%-10s'%'0008','%-10s' %'投影儀','%-10s' %'松下', '%10.2f' %1250.00, '%10d' %12) 22 print('%-10s'%'0009','%-10s' %'吸塵器','%-10s' %'飛利浦','%10.2f' %999.00, '%10d' %9) 23 print('-'*60) 24 25 # 商品數據 26 products = [ 27 ['0001', '電視機', '海爾', 5999.00, 20], 28 ['0002', '冰箱', '西門子', 6998.00, 15], 29 ['0003', '洗衣機', '小天鵝', 1999.00, 10], 30 ['0004', '空調', '格力', 3900.00, 0], 31 ['0005', '熱水器', '美的', 688.00, 30], 32 ['0006', '筆記本', '聯想', 5699.00, 10], 33 ['0007', '微波爐', '蘇泊爾', 480.50, 33], 34 ['0008', '投影儀', '松下', 1250.00, 12], 35 ['0009', '吸塵器', '飛利浦', 999.00, 9], 36 ] 37 38 # 用戶輸入信息 39 product_id = input('請輸入您要購買的產品編號:') 40 count = int(input('請輸入您要購買的產品數量:')) 41 l_product_id = list(product_id) 42 i = int(l_product_id[-1])-1 43 result = products[i][3]*count 44 45 # 獲取編號對應商品的信息 46 product_index = len(product_id)-1 47 product = products[product_index] 48 49 # 計算金額 50 print('購買成功,您需要支付',result,'元') 51 52 # 退出系統 53 print('謝謝您的光臨,下次再見!')
運行結果:

str_format:
1 """ 2 家用電器銷售系統 3 v1.0 4 """ 5 6 # 歡迎信息 7 print('歡迎使用家用電器銷售系統!') 8 9 # 產品信息列表 10 print('產品和價格信息如下:') 11 print('*'*60) 12 print('{:<10s}'.format('編號'),'{:<10s}'.format('名稱'),'{:<10s}'.format('品牌'),'{:<10s}'.format('價格'),'{:<10s}'.format('庫存數量')) 13 print('-'*60) 14 print('{:<10s}'.format('0001'),'{:<10s}' .format('電視機'),'{:<10}'.format('海爾'), '{:<10.2f}'.format(5999.00), '{:<10d}'.format(20)) 15 print('{:<10s}'.format('0002'),'{:<10s}' .format('冰箱'),'{:<10}'.format('西門子'), '{:<10.2f}'.format(6998.00), '{:<10d}'.format(15)) 16 print('{:<10s}'.format('0003'),'{:<10s}' .format('洗衣機'),'{:<10}'.format('小天鵝'), '{:<10.2f}'.format(1999.00), '{:<10d}'.format(10)) 17 print('{:<10s}'.format('0004'),'{:<10s}' .format('空調'),'{:<10}'.format('格力'), '{:<10.2f}'.format(3900.00), '{:<10d}'.format(0)) 18 print('{:<10s}'.format('0005'),'{:<10s}' .format('熱水器'),'{:<10}'.format('美的'), '{:<10.2f}'.format(688.00), '{:<10d}'.format(30)) 19 print('{:<10s}'.format('0006'),'{:<10s}' .format('筆記本'),'{:<10}'.format('聯想'), '{:<10.2f}'.format(5699.00), '{:<10d}'.format(10)) 20 print('{:<10s}'.format('0007'),'{:<10s}' .format('微波爐'),'{:<10}'.format('蘇泊爾'), '{:<10.2f}'.format(480.50), '{:<10d}'.format(33)) 21 print('{:<10s}'.format('0008'),'{:<10s}' .format('投影儀'),'{:<10}'.format('松下'), '{:<10.2f}'.format(1250.00), '{:<10d}'.format(12)) 22 print('{:<10s}'.format('0009'),'{:<10s}' .format('吸塵器'),'{:<10}'.format('飛利浦'), '{:<10.2f}'.format(999.00), '{:<10d}'.format(9)) 23 print('-'*60) 24 25 # 商品數據 26 products = [ 27 ['0001', '電視機', '海爾', 5999.00, 20], 28 ['0002', '冰箱', '西門子', 6998.00, 15], 29 ['0003', '洗衣機', '小天鵝', 1999.00, 10], 30 ['0004', '空調', '格力', 3900.00, 0], 31 ['0005', '熱水器', '美的', 688.00, 30], 32 ['0006', '筆記本', '聯想', 5699.00, 10], 33 ['0007', '微波爐', '蘇泊爾', 480.50, 33], 34 ['0008', '投影儀', '松下', 1250.00, 12], 35 ['0009', '吸塵器', '飛利浦', 999.00, 9], 36 ] 37 38 # 用戶輸入信息 39 product_id = input('請輸入您要購買的產品編號:') 40 count = int(input('請輸入您要購買的產品數量:')) 41 l_product_id = list(product_id) 42 i = int(l_product_id[-1])-1 43 result = products[i][3]*count 44 45 # 獲取編號對應商品的信息 46 product_index = len(product_id)-1 47 product = products[product_index] 48 49 # 計算金額 50 print('購買成功,您需要支付',result,'元') 51 52 # 退出系統 53 print('謝謝您的光臨,下次再見!')
運行結果:

f-string:
1 """ 2 家用電器銷售系統 3 v1.0 4 """ 5 6 # 歡迎信息 7 print('歡迎使用家用電器銷售系統!') 8 9 # 產品信息列表 10 print('產品和價格信息如下:') 11 print('*'*60) 12 a = ['編號','名稱','品牌','價格','庫存數量'] 13 b = ["0001","0002","0003","0004","0005","0006","0007","0008","0009"] 14 c = ['電視機','冰箱','洗衣機','空調','熱水器','筆記本','微波爐','投影儀','吸塵器'] 15 d = ['海爾','西門子','小天鵝','格力','美的','聯想','蘇泊爾','松下','飛利浦'] 16 e = [5999.00,6998.00,1999.00,3900.00,688.00,5699.00,480.50,1250.00,999.00] 17 f = [20,15,10,0,30,10,33,12,9] 18 19 print(f'{a[0]:10s}{a[1]:10s}{a[2]:10s}{a[3]:10s}{a[4]:10s}') 20 print('-'*60) 21 print(f'{b[0]:10s}{c[0]:10s}{d[0]:10s}{e[0]:10.2f}{f[0]:10d}') 22 print(f'{b[1]:10s}{c[1]:10s}{d[1]:10s}{e[1]:10.2f}{f[1]:10d}') 23 print(f'{b[2]:10s}{c[2]:10s}{d[2]:10s}{e[2]:10.2f}{f[2]:10d}') 24 print(f'{b[3]:10s}{c[3]:10s}{d[3]:10s}{e[3]:10.2f}{f[3]:10d}') 25 print(f'{b[4]:10s}{c[4]:10s}{d[4]:10s}{e[4]:10.2f}{f[4]:10d}') 26 print(f'{b[5]:10s}{c[5]:10s}{d[5]:10s}{e[5]:10.2f}{f[5]:10d}') 27 print(f'{b[6]:10s}{c[6]:10s}{d[6]:10s}{e[6]:10.2f}{f[6]:10d}') 28 print(f'{b[7]:10s}{c[7]:10s}{d[7]:10s}{e[7]:10.2f}{f[7]:10d}') 29 print(f'{b[8]:10s}{c[8]:10s}{d[8]:10s}{e[8]:10.2f}{f[8]:10d}') 30 print('-'*60) 31 32 # 商品數據 33 products = [ 34 ['0001', '電視機', '海爾', 5999.00, 20], 35 ['0002', '冰箱', '西門子', 6998.00, 15], 36 ['0003', '洗衣機', '小天鵝', 1999.00, 10], 37 ['0004', '空調', '格力', 3900.00, 0], 38 ['0005', '熱水器', '美的', 688.00, 30], 39 ['0006', '筆記本', '聯想', 5699.00, 10], 40 ['0007', '微波爐', '蘇泊爾', 480.50, 33], 41 ['0008', '投影儀', '松下', 1250.00, 12], 42 ['0009', '吸塵器', '飛利浦', 999.00, 9], 43 ] 44 45 # 用戶輸入信息 46 product_id = input('請輸入您要購買的產品編號:') 47 count = int(input('請輸入您要購買的產品數量:')) 48 l_product_id = list(product_id) 49 i = int(l_product_id[-1])-1 50 result = products[i][3]*count 51 52 # 獲取編號對應商品的信息 53 product_index = len(product_id)-1 54 product = products[product_index] 55 56 # 計算金額 57 print('購買成功,您需要支付',result,'元') 58 59 # 退出系統 60 print('謝謝您的光臨,下次再見!')
運行結果:

浙公網安備 33010602011771號