Python 基础学习Chapter6
生活随笔
收集整理的這篇文章主要介紹了
Python 基础学习Chapter6
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 基礎知識
- 1. 列表是什么
- 1.1 訪問列表元素
- 1.2 索引從0而不是1開始
- 1.3 索引可以為負值
- 2.列表中的方法
- 2.1 添加元素
- 2.1.1 在列表末尾插入從末尾插入
- 2.1.2 在列表中間插入
- 3. 刪除元素
- 3.1 del刪除
- 3.2 pop刪除
- 練習題
- 1.列表去重
- 2. 基本數值統計計算
- 3. 數據清洗
- 4. 密碼生成器
基礎知識
1. 列表是什么
biclye = ['lisy','cannondale','redline'] print(biclye) ['lisy', 'cannondale', 'redline']1.1 訪問列表元素
biclye = ['lisy','cannondale','redline'] print(biclye[0]) lisy1.2 索引從0而不是1開始
biclye = ['lisy','cannondale','redline'] print(biclye[1]) cannondale1.3 索引可以為負值
biclye = ['lisy','cannondale','redline'] print(biclye[-1]) redline2.列表中的方法
2.1 添加元素
2.1.1 在列表末尾插入從末尾插入
biclye = ['lisy','cannondale','redline'] biclye.append("ducati") print(biclye) ['lisy', 'cannondale', 'redline', 'ducati']2.1.2 在列表中間插入
biclye = ['lisy','cannondale','redline'] biclye.insert(2,'jack') print(biclye) ['lisy', 'cannondale', 'jack', 'redline']3. 刪除元素
3.1 del刪除
biclye = ['lisy','cannondale','redline'] del biclye[0] print(biclye) ['cannondale', 'redline']3.2 pop刪除
biclye = ['lisy','cannondale','redline']# pop的返回值是刪除的元素,默認從末尾刪除poped_motorcycles = biclye.pop() print(biclye) print(poped_motorcycles) ['lisy', 'cannondale'] redline練習題
1.列表去重
city = [‘上海’, “廣州”, “上?!? “成都”, “上海”, “上?!? “北京”, “上?!? “廣州”, “北京”, “上?!盷
(1)以city列表為操作數據,對列表數據進行去重處理
(2)打印輸出去重后的列表
city = ['上海', "廣州", "上海", "成都", "上海", "上海", "北京", "上海", "廣州", "北京", "上海"] list = set(city) print(list) {'上海', '廣州', '北京', '成都'}2. 基本數值統計計算
from math import sqrt def getn():nums=[]str=input("請輸入數字(回車退出):")while str!="":nums.append(eval(str))str=input("請輸入數字(回車退出):")return numsdef mide(num):s=0.0for n in num:s=s+nreturn s/len(num)def dev(num,m):sd=0.0for n in num:sd=sd+(n-m)**2return sqrt(sd/(len(num)-1))def med(num):new=sorted(num)lng=len(num)if lng%2==0:md=(new[lng//2-1]+new[lng//2])/2else:md=new[lng//2]return md n=getn() m=mide(n) print("平均值:{},標準差:{:.2},中位數{}".format(m,dev(n,m),med(n))) 請輸入數字(回車退出):2 請輸入數字(回車退出):3 請輸入數字(回車退出):6 請輸入數字(回車退出): 平均值:3.6666666666666665,標準差:2.1,中位數33. 數據清洗
去除數據中的空字符串、None、特殊符號等,只留下單詞及序號,并去重。
原始數據:
[" pizza,100001"," None"," alex,100002 “,“egon,100003”,” ",“egg,100004+pig,100005”]
清洗后數據:
[‘pizza,100001’, ‘pig,100005’, ‘alex,100002’, ‘egg,100004’, ‘egon,100003’]
content=[" pizza,100001"," None"," alex,100002 ","egon,100003"," ","egg,100004+pig,100005"] total_list=[] for i in content:temp_str=i.strip()if temp_str=="None"or temp_str=="":passelse:listl = temp_str .split("+")total_list.extend(listl) new_list = list(set(total_list)) print(new_list) ['pizza,100001', 'alex,100002', 'egon,100003', 'egg,100004', 'pig,100005']4. 密碼生成器
密碼必須包含大小寫字母、特殊字符、數字 p密碼位數:8-20位不等
要求同時產生100個密碼到列表
打印輸出所有密碼
import random def password_generation(password_length):result = []for i in range(password_length):if i % 4 == 2:result.append(random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ'))if i % 4 == 0:result.append(random.choice('1234567890'))if i % 4 == 1:result.append(random.choice('abcdefghijklmnopqrstuvwxyz'))if i % 4 == 3:result.append(random.choice('!$%()+,-.:;>?@[]`{}'))random.shuffle(result)result_str="".join(result)return result_strdef pwd_generation(num):pwd_list=[]for n in range(num):pwd_length=random.randint(8,21)pwd=password_generation(pwd_length)pwd_list.append(pwd)pwd_set=set(pwd_list)pwd_list1=list(pwd_set)return pwd_list1 pwd_list_make1=pwd_generation(100) for pwd in pwd_list_make1:print(pwd) @j0S53La) 8Q-Y9Fi3ru%7f(.K ?+3pPp[4TQqC7$1r O7:u81hYW9mD$>r}0nI :mS3]4w+2bnLc6K0L. YTbt26{xJ6,[Y2a[X@o4 4A[!f8Yh L>Hj>6q7Ee1 3O8hCn`0@Eg 9Tb[6?Kf ,8]gz6NBu7Pq0% 7TgqK:aI`90 %l1b82pVJL{ 8)AwI6]p Rcyf@[L29@2]F4nV 9flOX6`2n.Y v0rN25v?:I a3d1D[X}o1 Xw1-m124)U.f:3JFywT qMKt?{58W.1dbK7 f0Ts3Ex4k%T09N!$5Q;q> 1kc3SuD{?0 ,+1DpL020MS8wdn?) {803H>eT8r]kff4GQK! >Zt3z8(W-[3sPlD6 Kb77`b7D) n7Z@qN86v(X CKx4k,93jj2J!>+K wf8JaG,5%T$5Ue6} `0HL3J-[86ngu B(ktl>-n84S!2U3H rpEi}![6h8M2y8VP5M[:5 V7,6d?J0z(Bg(oB9+12Ey iALd5L1z9?. P`$@;aoctTXe6743{MH9 w8R73]}2HypQ0Re>q) %3IaE7s?e3M@qM0 Rs4w;M)1i80j?2CPk. :lX1.4Wj 6DGEu`f.0r5d3] 52vt(P.F R8;8Or%h2 EfW+g4Cg3[9 o8{Gt7Lv).5ZIr9%)Z1m KpS:p7;]87$Mp33auHE( n3P,M22m{ daP6Fn35}` M,o49r6+B bAz%%Oi0.C90 {nV5,x39YhG 157%fwL$;U96Ty]Tkh kHjG+h`>{H525p7s1L 00zY)2Ap) n0>{9AC2uj O3v-2gP%8 J5,B3o+b +82JciXu6? I}f5;p6Q hWw1m6`[9mNS?P`7 Ip+W,E:c3706s `J;8s1+lDAB25Gv;0@lz kyH29N9@(}gmHq%%U4E7 Asu)H]l?4H3O466cn+ U?Q47).L6gls }a9N,nKA$8-ow4C4 99B41>ky@ERH(?u8m k++2la?rVe492C4I;O VBtD0Bv.43K`56ek0!x`> H9@Yo@sPgT4.6d4]16;iP a;Fj!G38 1nr>Rz6E,06gUc!0)O6L. R7v!9M2}i ``rf2Ut6xX1})63YYc)S nRgEG`854%ac0U@;xZ0 ]b87$P8vZx (p4!4VxD 4T2%Mi,z1f q9R0!aSe%U0] i6?LJ:w28q Jqx2{X%):1k9N96lK 24Oq@j{q5C17Md-;C 0.wV)K91n o6ptJg,l%2R]757L!M 46ecCU+>5y BrwN;!992d l3f0Tst$J65P:Z) 3Fho4Suh})@5$O90U 3nLD5Zysc31(@(C b5J[?m053Ze`lY2X! -5igQ7hR8X;]7Mo9!k Jd3Rl5j@9! q9{iu]OE58l6V, 1[LzihU0@R,M17be]2 GQa0.8b$1Kq %3Xlt0y[%27AdB %VY40a6?v ,4CUh!4Z;ypz7B5N6>7p> W?w1(P{3z(zG89Pc n4@2q]dc+3M3GC0uQ, 2%u:7L5yJ總結
以上是生活随笔為你收集整理的Python 基础学习Chapter6的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java.nio.file 找不到_ja
- 下一篇: java虚拟机06-内存分区/新生代、老