# python 選TEST 密碼 vdsl 1. a=2 #整數 b=15.0 #浮點數 n='LCC'#字串 ``` 2. ## 算術運算子 + - * / %(取餘數) //(取商) **(次方) ``` ```p num1 = 222 num2 = 333 num3 = num1 + num2 print('相加後結果',num3) print('相加後結果',(num1 + num2)) print('2的5次方:',2**5) print('取餘數的結果',5%2) ``` 相加後結果 555 相加後結果 555 2的5次方: 32 取餘數的結果 1 3. ## 格式化輸出 %s (字串) %d(整數) %s(浮點數) ```p score = 90.5 name = 'LCC' count = 1 print('%s第%d次物理考試成績%.2f分'%(name,count,score)) ``` LCC第1次物理考試成績90.50分 ```p name = '蔡宗宸' age = 29 weight = 65.0 print('我叫%s今年%d,體重%.2f'%(name,age,weight)) ``` 我叫蔡宗宸今年29,體重65.00 4. ## 關係運算子 < > <= >= ==(等於) !=(不等於) 指定運算子 += -= *= /= %= //= 邏輯運算子 and 所有為真即為真 or 只要有一個是真就是真 example: a+=b(a=a+b) a*=b(a=a*b) 5. ## if 敘述 if(判斷條件): 程式敘述 if(判斷條件): # elif(判斷條件): # else: pass ```python= age = 17 if(age<18): print('年齡未滿18歲不能購買香菸') ``` 年齡未滿18歲不能購買香菸 ```python= name = input('請輸入名字') age = int(input('請輸入年齡')) name,age ``` 請輸入名字123 請輸入年齡35 out('123', 35) ```python= age = int(input('請輸入年齡')) if(age<18): print('未滿18歲,無法購買香菸') else: print('可以購買香菸') ``` 請輸入年齡10 未滿18歲,無法購買香菸 ```python= print('計算票價') age=int(input('請輸入年齡:')) ticket=100 if(age>=80 or age<=6): ticket=ticket*0.2 print('票價是%d'%(ticket)) elif(age>=60 or age<=12): ticket*=0.5 print('票價是%d'%(ticket)) else: print('票價是%d'%(ticket)) ``` 計算票價 請輸入年齡:50 票價是100 6. ## 串列 list list_name=[ , , , ] 串列可以有整數 浮點數 tuple(元組) dict(字典) list 串列的切片 list_name[:] 讀取全部 list_name[n:] 從n到最後 list_name[:n] 從0到n list_name[n:n] 從n到n list_name[-n:] -n到最後 ``` [star:end-1:step](step預設為1) 正向排列由0開始 → 負向排列由-1開始 ← ``` ```python= james=[39,75,68,27,43,51,12] james[:] james[2:] james[:4] james[1::2] james[0::3] james[-4:] james[-6:-3] james[-6::2] ``` 結果分別為 [39, 75, 68, 27, 43, 51, 12] [68, 27, 43, 51, 12] [39, 75, 68, 27] [75, 27, 51] [39, 27, 12] [27, 43, 51, 12] [75, 68, 27] [75, 27, 51] ```python= worriors=['Curry','Durant','Iguodla','Bell','Tompson'] print('前三名球員:',worriors[:3]) print('球員索引1到最後:',worriors[1:]) print('後三名球員:',worriors[-3:]) ``` 前三名球員: ['Curry', 'Durant', 'Iguodla'] 球員索引1到最後: ['Durant', 'Iguodla', 'Bell', 'Tompson'] 後三名球員: ['Iguodla', 'Bell', 'Tompson'] 串列的語法 max() min() sum() 加總 append() 從末端加入 insert(index,obiect) 從指定位置加入 pop()移除物件(預設從最後 後進先出) remove() 移除第一個出現 reverse()反轉 (頭尾互換...) sort() 排序,原值會被改變 sorted() 排序,原值會改變 count() 計算物件出現次數 clear() 清空 ```python= num=[1,2,3,4,5,6] cars=[] cars.append('Honda') cars.insert(1,'BMW') print('找出num中最大值:',max(num)) print('找出num中最小值:',min(num)) print('cars:',cars) cars.append('toyota') cars.pop() print('cars:',cars) ``` 找出num中最大值: 6 找出num中最小值: 1 cars: ['Honda', 'BMW'] cars: ['Honda', 'BMW'] ```python= numbers=[100,200,300,400,500] number=numbers.remove(300) print('number:',number) print('numbers:',numbers) num.reverse() print('num:',num) cars.clear() print('cars:',cars) ``` number: None numbers: [100, 200, 400, 500] num: [1, 2, 3, 4, 5, 6] cars: [] 傳址 ```python= a=[1,2,3] a ------------------- b=a b ------------------- a[0]='surprise' a ------------------- b ``` 顯示結果 [1, 2, 3] ------------------- [1, 2, 3] ------------------- ['surprise', 2, 3] ------------------- ['surprise', 2, 3] ------------------- 不被取代的方法 ```python= a=[1,2,3] b=a.copy() c=list(a) d=a[:] a[0]='list are boring' a -------------------- b -------------------- c -------------------- d ``` 顯示結果 ['list are boring', 2, 3] -------------------- [1, 2, 3] -------------------- [1, 2, 3] -------------------- [1, 2, 3] -------------------- ```python= star='Na'*4+'\n' middle='Hey'*3+'\n' end='GoodBye' print(star+middle+end) ``` NaNaNaNa HeyHeyHey GoodBye # +號僅限於使用在字串 ```python= print('*'*40) ``` **************************************** # *號也可以 指定位移值 ```python= letter='iejfkldjfklcmkldfjjdsfkljdklfjl' letter[6] ``` 'd' ```python= letter[-1] ``` 'l' 找出隨機字串中的值 正向反向都可以用 ```python= name='Happy' print(name.replace('H','P')) print(name) ``` Pappy Happy 取代字串 len()長度 split('為切割目標') 可以為空白 ```python= todos='get gloves, get mask, jet wool sweater, get mittens' print(todos.split(',')) ``` ['get gloves', ' get mask', ' jet wool sweater', ' get mittens'] ```python= todos='get gloves, get mask, jet wool sweater, get mittens' print(todos.split(' ')) ``` ['get', 'gloves,', 'get', 'mask,', 'jet', 'wool', 'sweater,', 'get', 'mittens'] ```python= splitme='a/b//c/d///e' print(splitme.split('/')) ``` ['a', 'b', '', 'c', 'd', '', '', 'e'] ```python= poem = 'Margaret was viewed by her contemporaries as being rather eccentric. She was extravagent and flirtatious, accused of using speech full of oaths and obscenity, and was noted for her unusual sense of fashion. This reputation for eccentricity survives today, when Margaret is widely referred to as Mad Madge.' poem[:13] ``` ```python= len(poem) ``` 305 ```python= #開頭是不是? poem.startswith('All') ``` False ```python= #結尾是不是? poem.endswith('as Mad Madge.') ``` True ```python= #這單字第一次出現的位置 poem.find('This')#只會找回第一筆 ``` 207 ```python= maxxes=['kitty','monica','Jordon','John','Lisa'] minxes=['Karl','They'] #透過append()會被以list方式置入 maxxes.append(minxes) maxxes ``` ['kitty', 'monica', 'Jordon', 'John', 'Lisa', ['Karl', 'They']] ```python= #找出某單字是否在list中 numbers=['We','you','They','He','His'] 'He' in numbers ``` True 7. ## for 迴圈 for 變數名稱 in range(開始,結束-1,step): for 變數名稱 in listname: ```python= #從1加到10 sum = 0 for i in range(1,11): sum+=i print(sum) ``` 1 3 6 10 15 21 28 36 45 55 ```python= sum = 0 for i in range(1,11): sum+=i print(sum) ``` 55 ```python= #從1加到某數(自行輸入) sum=0 a=int(input('請輸入一個數字:')) for i in range(1,a+1): sum+=i print('從1加到%d的總和=%d'%(a,sum)) ``` 請輸入一個數字:10 從1加到10的總和=55 ```python= #從1到10的平方分別放入串列裡 squars=[]#空串列 n=int(input('請輸入一個整數:')) if(n>10): n=10 for num in range(1,n+1): value=num**2 squars.append(value) print(squars,end=' ')#end=(' ') 不換行 ``` 請輸入一個整數:20 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] ```python= #購買金額超過1000元,就放入vip串列,未滿1000元的放入buy串列 vip=[] buy=[] buyers=[['James',1002],['Monica',2500],['Kitty',900],['John',3200],['Mary',150]] for i in buyers: if(i[1]>1000): vip.append(i[0]) else: buy.append(i[0]) print(vip) print(buy) ``` ['James', 'Monica', 'John'] ['Kitty', 'Mary'] ```python= #從1到100可以被3與7整除的數 for i in range(1,101): if(i%3==0 and i%7==0): print(i,end=' ') ``` 21 42 63 8. ## 生成式是以一個或多個迭代器密集建立PYTHON資料結構的方式 串列生成式 #[運算式 for 項目 in 可跌帶項目] #[運算式 for 項目 in 可跌帶項目 if 條件式] numberlist=[number for number in range(1,6)] numberlist ```python= #用zip迭代多個序列 days=['Monday','Tuesday','Wednesday'] fruits=['banana','orange','apple'] drinks=['coffee','beer','soda','tea'] desserts=['ice cream','pie','puddin'] for days,fruits,drinks,desserts in zip (days,fruits,drinks,desserts): print(days,':drink',drinks,':fruit',fruits,':dessert',desserts) ``` Monday :drink coffee :fruit banana :dessert ice cream Tuesday :drink beer :fruit orange :dessert pie Wednesday :drink soda :fruit apple :dessert puddin ```python= list=[number for number in range(0,10) if number%2==0] list ``` [0, 2, 4, 6, 8] ```python= #雙層迴圈 rows=range(1,4) cols=range(1,3) for row in rows: for col in cols: print(row,col) ``` 1 1 1 2 2 1 2 2 3 1 3 2 ```python= #生成式 rows=range(1,4) cols=range(1,3) cells=[(row,col) for row in rows for col in cols] cells ``` [(1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2)] ```python= #九九乘法表 for i in range(1,10): for j in range(1,10): result=i*j print('%d*%d=%-3d'%(i,j,result),end=' ') print(' ') ``` 9. ## continue,break 此函數一定要在有條件式的迴圈下使用 ```python= #break for digit in range(1,11): if digit%3==0: break print(digit,end=' ') ``` 1 2 ```python= #continue for digit in range(1,11): if digit%3==0: continue print(digit,end=' ') ``` 1 2 4 5 7 8 10 ```python= players=['Kitty','Kevin','Tom','John','Monica','Mary','Jordan'] n=int(input('請輸入人數:')) if n>len(players): n=len(players) index=0 for player in players: if index==n: break print(player,end=' ') index+=1 print('共%d人'%index) ``` 請輸入人數:4 Kitty Kevin Tom John 共4人 ```python= scores=[33,22,41,25,39,43,27,38,40] game=0 #請印出超過30分的場次 for score in scores: if score<=30: continue print(score,end=' ') game+=1 print('超過30分以上共%d場'%game) ``` 33 41 39 43 38 40 超過30分以上共6場 ```python= players=[['James',202],['Kitty',190],['Monica',205],['Jordan',211],['Mary',193]] #列出身高市200公分以上的球員 for player in players: if player[1]<200: continue print(player[0],end=' ') ``` James Monica Jordan 10. ## while 迴圈 設定初值 while 判斷條件: 程式敘述 設定增減量 ```python= msg1='人機對話專欄,我會重覆你說的話喔!' msg2='輸入q可以結束對話...' msg3=msg1+'\n'+msg2+'\n'+'=' input_msg='' while input_msg!='q': input_msg=input(msg3) if input_msg!='q': print(input_msg) ``` 人機對話專欄,我會重覆你說的話喔! 輸入q可以結束對話... =123 123 人機對話專欄,我會重覆你說的話喔! 輸入q可以結束對話... =321 321 人機對話專欄,我會重覆你說的話喔! 輸入q可以結束對話... =1234567 1234567 人機對話專欄,我會重覆你說的話喔! 輸入q可以結束對話... =q ```python= #巢狀while迴圈的九九乘法表 i=1 while i<10: j=1 while j<10: result=i*j print('%d*%d=%-3d'%(i,j,result),end=' ') j+=1 print('') i+=1 ``` ## 隨機整數 inport random random.randint(0,99) //隨機存取0~99的整數 random.random() //隨機存取浮點數 random.choice(['A','B','C','D','E']) //隨機存取字串 [] 串列 () tuple#不能新增刪減 {:} 字典 {,} 集合 ```python= list01=[1,2,3,4,5,6] lc01=[2*x for x in list01] lc01 ``` [2, 4, 6, 8, 10, 12] ```python= #華氏溫度轉成攝氏(f-32)*5/9 f_list=[66.6,68,73.4,77.8,67,57.4,77.2] c_list=[(f-32)*5/9 for f in f_list] c_list #print(c_list,end=' ') ``` [19.222222222222218, 20.0, 23.000000000000004, 25.444444444444443, 19.444444444444443, 14.11111111111111, 25.11111111111111] ```python= from math import sqrt 學生成績=[56,67,45,31,90,67,10] 開根成績=[int(sqrt(x)*10) for x in 學生成績] 開根成績 ``` [74, 81, 67, 55, 94, 81, 31] ```python= stu_lot=[(56,78,67),(77,89,90),(66,80,80)] for i in stu_lot: avg=int(sum(i)/len(i)) print(avg,end=' ') ``` 67 85 75 ```python= stu_lot=[(56,78,67),(77,89,90),(66,80,80)] stu_avg=[int(sum(i)/len(i)) for i in stu_lot] stu_avg ``` [67, 85, 75] ```python= #串列與元組資料互換 #list():將元組資料轉換為list #tuple():將串列資料轉換為tuple keys=('magic','xaab',9099) #key.append(8588) list_keys=list(keys) list_keys.append(8588) print('列印串列資料:',list_keys) print('列印元組資料:',keys) ``` 列印串列資料: ['magic', 'xaab', 9099, 8588] 列印元組資料: ('magic', 'xaab', 9099) 11. ## 字典 ```python= sold={'tag':'red','score':3} sold['xpos']=100 sold['ypos']=30 sold['speed']='slow' print('x座標:',sold['xpos']) print('y座標:',sold['ypos']) print('移動速度:',sold['speed']) print(sold) ``` x座標: 100 y座標: 30 移動速度: slow {'tag': 'red', 'score': 3, 'xpos': 100, 'ypos': 30, 'speed': 'slow'} ```python= sports={'john':['足球','棒球'],'Monica':['排球','籃球','足球'],'Kitty':['棒球','排球']} for name,favorite in sports.items(): print('%s最喜歡的運動:'%name) for x in favorite: print(' ',x) ``` john最喜歡的運動: 足球 棒球 Monica最喜歡的運動: 排球 籃球 足球 Kitty最喜歡的運動: 棒球 排球 ```python= tol=(['26','aa'],['77','ae'],['ed','46'],['ds','77']) dict(tol) list=[['00',15],[55,'AA'],[15,'CC'],[45,'CC']] list_dict=dict(list) list_dict ``` {'26': 'aa', '77': 'ae', 'ed': '46', 'ds': '77'} {'00': 15, 55: 'AA', 15: 'CC', 45: 'CC'} key不可以一樣 但是value可以一樣 如果key一樣 後面的value會蓋掉前面的value ```python= pop_language={'C':'1972','python':'1991','Java':'1995'} pop_language['Java']='NO.1 in 1995' pop_language ``` {'C': '1972', 'python': '1991', 'Java': 'NO.1 in 1995'} ```python= another_language={'C#':'microsoft','java':'james 1995'} pop_language.update(another_language) pop_language ``` {'C': '1972', 'python': '1991', 'Java': 'NO.1 in 1995', 'C#': 'microsoft', 'java': 'james 1995'} ```python= del pop_language['java'] pop_language ``` {'C': '1972', 'python': '1991', 'Java': 'NO.1 in 1995', 'C#': 'microsoft'} ```python= another_language.clear() another_language ``` {} ```python= pop_language.get('R','not') ``` 'not' ```python= pop_language.get('python','not') ``` '1991' ```python= list_pop_language=list(pop_language.keys()) list_pop_language ``` ['C', 'python', 'Java', 'C#'] ```python= list_pop_language_value=list(pop_language.values()) list_pop_language_value ``` ['1972', '1991', 'NO.1 in 1995', 'microsoft'] ```python= pop_language2=pop_language.copy() pop_language2 ``` {'C': '1972', 'python': '1991', 'Java': 'NO.1 in 1995', 'C#': 'microsoft'} ```python= pop_language2['R']='R' pop_language ``` {'C': '1972', 'python': '1991', 'Java': 'NO.1 in 1995', 'C#': 'microsoft'} ```python= pop_language2 ``` {'C': '1972', 'python': '1991', 'Java': 'NO.1 in 1995', 'C#': 'microsoft', 'R': 'R'} ```python= Wechat_account={'hung':{'last_name':'李','first_name':'小明','city':'台北'},'chen':{'last_name':'王','first_name':'大華','city':'高雄'}} for account,account_info in Wechat_account.items(): print('使用者帽號:',account) print('姓名:',account_info['last_name'],account_info['first_name']) print('城市:',account_info['city']) ``` 使用者帽號: hung 姓名: 李 小明 城市: 台北 使用者帽號: chen 姓名: 王 大華 城市: 高雄 ```python= #update合併字典 pythons={'Green':'kitty','yellow':'John','Red':'Monica'} numbers={'A':1,'B':2,'C':3,'Green':'Mary'} pythons.update(numbers) pythons ``` {'Green': 'Mary', 'yellow': 'John', 'Red': 'Monica', 'A': 1, 'B': 2, 'C': 3} #Green的值被取代了 ```python= list(pythons.keys()) ``` ['Green', 'yellow', 'Red', 'A', 'B', 'C'] ```python= list(pythons.values()) ``` ['Mary', 'John', 'Monica', 1, 2, 3] ```python= #以set()建立集合 empty_set=set() empty_set ``` set() //set()不是{},{}是空字典 ```python= #用set()轉換,會丟棄重複的值 set_1=set('letters') set_1 ``` {'e', 'l', 'r', 's', 't'} ```python= len(set_1) ``` 5 12. ## 集合 set ```python= #串列轉集合 set(['a','b','c','d','d']) ``` {'a', 'b', 'c', 'd'} ```python= #tuple轉集合 set(('a','b','c','d','d')) ``` {'a', 'b', 'c', 'd'} ```python= #dict轉集合,只會用到鍵 set({'apple':'red','orange':'orange','banana':'yellow'}) ``` {'apple', 'banana', 'orange'} ```python= #集合生成式 #{運算式 for 運算式 in 可迭代項目} a_set={number for number in range(1,6) if number%3==1} a_set ``` {1, 4} ```python= #用in來測試值 drinks={'martin':{'vodka','vermouth'},'black russuan':{'vodka','kahlua'},'white russian':{'cream','kahlua','bitters'},'screwdrive':{'orange juice','vodka'}} for name,contents in drinks.items(): if 'vodka' in contents: print(name) ``` martin black russuan screwdrive ```python= #vodka裡面不含cream與vermouth drinks={'martin':{'vodka','vermouth'},'black russuan':{'vodka','kahlua'},'white russian':{'cream','kahlua','bitters'},'screwdrive':{'orange juice','vodka'}} for name,contents in drinks.items(): if 'vodka' in contents and not('cream' in contents or 'vermouth' in contents): print(name) ``` black russuan screwdrive 交集 intersection() 或著 & 同時在A集合和B集合中 ```python= set_a={'a','b','c','d'} set_b={'c','d','e','f'} intersection_a=set_a&set_b print(intersection_a) ``` {'d', 'c'} ```python= set_a.intersection(set_b) ``` {'c', 'd'} ```python= math={'Kevin','Peter','Monica'} eng={'Peter','John','Kitty','Kevin'} math & eng math={'Kevin','Peter','Monica'} eng={'Peter','John','Kitty','Kevin'} both=math & eng print(both) math={'Kevin','Peter','Monica'} eng={'Peter','John','Kitty','Kevin'} math.intersection(eng) ``` {'Kevin', 'Peter'} 聯集 | union() ```python= set_c={1,2,3,4,5} set_d={5,6,7,8,9} print(set_c|set_d) set_c.union(set_d) ``` {1, 2, 3, 4, 5, 6, 7, 8, 9} 差集 - difference() 在A中但是沒有在B中 A-B 不等於 B-A ```python= et_c={'a','b','c','d'} set_d={'c','d','e','f'} print(set_c-set_d) ``` {'b', 'a'} ```python= set_d.difference(set_c) ``` {'e', 'f'} 對稱差集 ^ symmetric_difference() 排除同時在AB中 ```python= set_c^set_d ``` {'a', 'b', 'e', 'f'} ```python= A={1,2,3,4,5} B={3,4,5,6,7} both=A^B AB=A.symmetric_difference(B) print(both) print(AB) ``` {1, 2, 6, 7} {1, 2, 6, 7} 13. ## 自定函式 函式可以精簡程式,到程式模組化 def function_name(arg1,arg2,...): (1)沒有傳入參數 ```python= def fa(): for i in range(1,10): print('*'*40) print('Hello Python') fa() ``` **************************************** Hello Python **************************************** Hello Python **************************************** Hello Python **************************************** Hello Python **************************************** Hello Python **************************************** Hello Python **************************************** Hello Python **************************************** Hello Python **************************************** Hello Python (2)傳入參數 ```python= def f(n): print(n) x='Hello World' f(x)#呼叫 f f('python') f(123) ``` Hello World python 123 代入參數並return結果 ```python= def g(n): tmp=1 for i in range(1,n+1): tmp=tmp*i return tmp x=g(5) print(x) print(g(10)) ``` 120 3628800 ```python= def f(x): a=0 for i in range(1,x+1): a+=i return a x=int(input('請輸入一個數:')) print('你輸入%d,總和是%d'%(x,f(x))) ``` 請輸入一個數:10 你輸入10,總和是55 ```python= def f(x,y): sum=x+y return sum a=int(input('請輸入第一個值')) b=int(input('請輸入第二個值')) print(f(a,b)) ``` 請輸入第一個值90 請輸入第二個值90 180 ```python= def f(x1,x2): x=x1+x2 return x a=int(input('請輸入第一個值:')) b=int(input('請輸入第二個值:')) print('你輸入的值是%d,%d 總和是%d'%(a,b,f(a,b))) ``` 請輸入第一個值:1 請輸入第二個值:2 你輸入的值是1,2 總和是3 ```python= def add(x1,x2): addresult=x1+x2 subresult=x1-x2 mulresult=x1*x2 divresult=x1/x2 return addresult,subresult,mulresult,divresult x1=x2=10 a,b,c,d=add(x1,x2) print('加法結果:',a) print('減法結果:',b) print('乘法結果:',c) print('除法結果:',d) ``` 加法結果: 20 減法結果: 0 乘法結果: 100 除法結果: 1.0 ```python= def add_sum(color): if color=='red': return 'It is tomato' elif color=='green': return 'It is green pepper' elif color=='bee purple': return 'I do not know what it is' else: return 'I have never heard of the color %s'%color print(add_sum('blue')) ``` I have never heard of the color blue ```python= #傳list def star(mylist=[]): h=max(mylist) return h number=[89,95,78,32,61,12,57] data=star(number) print(data) print('\n') print(star(number)) ``` 95 95 ```python= animal='rabbit'#全域變數 def print_global(): #在function中列印全域變數 print('inside print_global:',animal) #example 在 function中想要更改全域變數的值 def change_and_print_global(): print('inside change_and_print_global:',animal) #animal='cat'##會產生錯誤 print('after the change:',animal) #example直接在function中重新定義一個animal def change_and_print_local(): animal='dog' print('print animal:',animal) print_global() change_and_print_global() change_and_print_local() ``` inside print_global: rabbit inside change_and_print_global: rabbit after the change: rabbit print animal: dog ```python= animal='dog' def print_global(): print('inside print_global:',animal) print('at the top level:',animal) print_global() #example4可以global關鍵字,在function中更改全域變數值 def change_and_print_global2(): global animal animal='cat' print('change_and_global2:',animal) change_and_print_global2() print_global()#可以看到animal被重新定義 ``` at the top level: dog inside print_global: dog change_and_global2: cat inside print_global: cat ```python= #*會將可變數量的引數群組化成一個tuple #**會將可變數量的引數群組化成一個dict,名稱是key,對應的是value def f1(*args): print(args)#tuple f1(1,2,3,4,5,6,8,'Hello') f1('How','do','you','do') ``` (1, 2, 3, 4, 5, 6, 8, 'Hello') ('How', 'do', 'you', 'do') ```python= def f2(**kwargs): print(kwargs) f2(key1=1,key2=2,key3=3) ``` {'key1': 1, 'key2': 2, 'key3': 3} ```python= def f3(x,y,*args): print(x,y) print(args)#tuple f3(1,3,4,5,7,9,11,13,'Python') ``` 1 3 (4, 5, 7, 9, 11, 13, 'Python') ```python= #巢狀方法 w=21 def outer(x): def inner(y): z=w+x+y print(z) inner(1) outer(88) ``` 110 ```python= #Factory Function def maker(n): def action(x): return x*n return action f=maker(2) print(f) print(f(3)) print(f(4)) ``` <function maker.<locals>.action at 0x00000000052B8438> 6 8 ```python= #lambda #def fun(param1,param2,...): # return expression def func(x,y,z): return x+y+z print(func(1,2,3)) func2=lambda x,y,z:x+y+z print(func2(1,2,3)) ``` 6 6 14. # 例外處理 ```python= #Exception例外處理 def division(x,y): try: ans=x/y except ZeroDivisionError: print('除數不可為0') else: return ans print(division(10,2)) print(division(5,0)) ``` 5.0 除數不可為0 None ```python= list1=[1,2,3] position=5 list1[position] ``` IndexError Traceback (most recent call last) <ipython-input-4-6a05134a6d4d> in <module> 1 list1=[1,2,3] 2 position=5 ----> 3 list1[position] IndexError: list index out of range 解決方法 ```python= #例外處理 list1=[1,2,3] position=5 try: list1[position] except: print('Need a position between 0 and',len(list1)-1,'but got',position) ``` Need a position between 0 and 2 but got 5 15. # class是類別 有屬性與方法 ```python= class Puppy(): name='小黑' type='拉布拉多' def skill(self): print('拎拖鞋') dog=Puppy() print('Dog name:',dog.name) print('Dog type:',dog.type) dog.skill() ``` Dog name: 小黑 Dog type: 拉布拉多 拎拖鞋 ```python= class Bank(): title='Taipei' def motto(self): return '以客為尊' a=Bank() print('銀行的位置在:',a.title) print('銀行服務的宗旨是',a.motto()) ``` 銀行的位置在: Taipei 銀行服務的宗旨是 以客為尊 ```python= class Car(): name1='Toyota' name2='BMW' def tspeed(self): return 150 def bspeed(self): return 200 a=Car() print('%s時數%d'%(a.name1,a.tspeed()),end=' ') print('%s時數%d'%(a.name2,a.bspeed())) ``` Toyota時數150 BMW時數200 ```python= class Fish(): name='小金' type1='金魚' def skill(self): return '吐泡泡' a=Fish() print('我有一隻',a.type1,',他的名字叫',a.name,',每當它肚子餓的時候都會',a.skill()) ``` 我有一隻 金魚 ,他的名字叫 小金 ,每當它肚子餓的時候都會 吐泡泡 ```python= class Ball(): def set_radius(self,radius): self.r=radius def get_num(self):#計算球體體積 num=4/3*3.14*(self.r*self.r*self.r) return num def get_area(self):#計算球體表面積 self.area=4*3.14/(self.r*self.r) x=Ball() x.set_radius(5) #呼叫計算球體體積 print('球體體積:',x.get_num()) #呼叫計算球體表面積 x.get_area() print('球體表面積:',x.area) ``` 球體體積: 523.3333333333334 球體表面積: 0.5024000000000001 這題的重點是把所有屬性前面加self.這樣就不會有任何問題 不加的原因是num有return ```python= class Car(): def set_carname(self,name): self.c=name def get_speed(self): num=150 return num def get_type(self): self.type1='Camary 房車' c=Car() car1=input('請輸入汽車廠牌:') c.set_carname(car1) print('%s的時速:%d'%(car1,c.get_speed())) c.get_type() print('車型:%s'%c.type1) ``` 請輸入汽車廠牌:toyota toyota的時速:150 車型:Camary 房車 ```python= #存款與提款 class Banks(): #類別初始值 title='Taipei Bank' def __init__(self,uname,money): self.name=uname self.balance=money def save_money(self,money):#存款 self.balance+=money print('存款',money,'完成') def withdraw_money(self,money):#提款 self.balance-=money print('提款',money,'完成') def get_balance(self): print(self.name,'目前餘額',self.balance) hb=Banks('hung',30000) hb.get_balance() hb.save_money(10000) hb.get_balance() hb.withdraw_money(1000) hb.get_balance() ``` hung 目前餘額 30000 存款 10000 完成 hung 目前餘額 40000 提款 1000 完成 hung 目前餘額 39000 ```python= class Car(): def __init__(self,name): self.name='Car'+name def speed(self,num): print('時速是:',num) class BMWCar(): def __init__(self,name): self.name='Car'+name def speed(self,num): print('時速是:',num) class AudiCar(): def __init__(self,name): self.name='Car'+name def speed(self,num): print('時速是:',num) c=Car('Car') b=BMWCar('BMW') a=AudiCar('Audi') print(c.name,end=' ') c.speed(100) print(b.name,end=' ') b.speed(200) print(a.name,end=' ') a.speed(250) ``` CarCar 時速是: 100 CarBMW 時速是: 200 CarAudi 時速是: 250 16. # 封裝 私有屬性→在屬性前面加上__ 私有方法→在方法前面加上__ 要存取私有屬性有2種方式 1.選字get()和set()方法 2.物件名稱._類別名稱.私有屬性 要存取私有方法 1.物件名稱._類別名稱.私有方法() 封裝主要是維護內部資料的存取安全 ```python= class Banks(): def __init__(self,name): self.__name=name self.__balance=0 self.__bank_name='Taipei Bank' def save_money(self,dollar): self.__balance+=dollar print(self.__name,'存入了',dollar) def withdraw(self,dollar): self.__balance-=dollar print(self.__name,'提款',dollar) def get_balance(self): print(self.__balance) p=Banks('Kitty') p.get_balance() #p.balance=10000 p.save_money(10000) p.withdraw(5000) p._Banks__balance=1000 p.get_balance() ``` 0 Kitty 存入了 10000 Kitty 提款 5000 1000 1000是因為直接取代balance ```python= class Person(): def __init__(self,name): self.__name=name def getname(self): return self.__name def setname(self,newname): if len(newname)>5: self.__name=newname else: print('名字長度要大於5...') x=Person('John') print(x._Person__name) x.setname('Kitty') print(x.getname()) ``` John 名字長度要大於5... John ```python= #私有方法 class Banks(): def __init__(self,name): self.__name=name self.__balance=1000 self.__bankname='Kaohsiung Bank' self.__rate=30 def __change(self,money): return int(money*self.__rate) m=Banks('Hung') print(m._Banks__change(5000)) print(m._Banks__name,'更換後金額:',m._Banks__change(8000)) ``` 150000 Hung 更換後金額: 240000 17. # 繼承 class 父類別(): #基底類別 statement class 子類別(要繼承的父類別): #衍生類別 statement 1.子類別只能繼承一個父類別 2.同一個父類別可以被好幾個子類別繼承 ```python= class Car(): def exclaim(self): print('I am a car') class Yugo(Car): pass give_me_a_car=Car() give_me_a_Yugo=Yugo() give_me_a_car.exclaim() give_me_a_Yugo.exclaim() ``` I am a car I am a car ```python= class Person(): def __init__(self,name): self.name=name class MDPerson(Person): def __init__(self,name): self.name='Doctor '+name class JDPerson(Person): def __init__(self,name): self.name='Teacher '+name p=Person('John') m=MDPerson('Kitty') j=JDPerson('Monica') print(p.name) print(m.name) print(j.name) ``` John Doctor Kitty Teacher Monica ```python= class Banks(): def __init__(self,name): self.__name=name self.__balance=100000 self.__title='Taipei Bank' def save_money(self,money): self.__balance+=money print('存款',money,'完成') def with_draw_money(self,money): self.__balance-=money print('提款',money,'完成') def getbalance(self): print(self.__name,'目前餘額',self.__balance) class Shilin_Banks(Banks): pass a=input('請輸入存款人名字:') h=Shilin_Banks(a) h.save_money(int(input('您要存多少錢:'))) h.with_draw_money(5000) h.getbalance() ``` 輸入存款人名字:HUNG 您要存多少錢:10000 存款 10000 完成 提款 5000 完成 HUNG 目前餘額 105000 ```python= #衍生類別引用基底類別的屬性使用super #super class Person(): def __init__(self,name): self.name=name+' Father' class EmailPerson(Person): def __init__(self,name,email): super().__init__(name)#呼叫父類別__init__ self.email=email+' son' bob=EmailPerson('John',"1234@gmail.com") print(bob.name)#John Father print(bob.email)#1234@gmail.com son ``` John Father 1234@gmail.com son ```python= class A(): def spam(self): print('A spam') class B(A): def spamB(self): super().spam() print('B spam') a=B() a.spamB() ``` A spam B spam ```python= class A(): def add(self,x): y=x+1 print(y) class B(A): def add(self,x): super().add(x) a=B() num=int(input('請輸入一個整數:')) a.add(num) ``` 請輸入一個整數:4 5 ```python= class A(): def spam(self): print('A spam') class B(A): def __init__(self): super().spam() print('B spam') a=B() ``` A spam B spam #__init__類別初始化 會直接執行不論有沒有傳值 ```python= class GrandFather(): """定義組父的資產""" def __init__(self): self.grandfathermoney=100000 def get_info1(self): print('GrandFather') class Father(GrandFather): """定義父親的資產""" def __init__(self): self.fathermoney=50000 super().__init__() def get_info2(self): print('Father') class Ivan(Father): """定義兒子的資產""" def __init__(self): self.ivanmoney=20000 super().__init__() def get_info3(self): print('Son') def get_money(self): print('\nIvan資產:',self.ivanmoney,'\n父親的資產:',self.fathermoney,'\n組父的資產:',self.grandfathermoney) p=Ivan() p.get_money() p.get_info1() p.get_info2() p.get_info3() ``` Ivan資產: 20000 父親的資產: 50000 組父的資產: 100000 GrandFather Father Son ```python= #if __name__=='__main__': class FoodParent(): def __init__(self): print('FoodParent') def bar(self,message): print('%s FoodParent'%message) class FoodSon(FoodParent): def __init__(self): super().__init__() print('Son') def bar(self,message): super().bar(message) print('Son message') if __name__=='__main__': s=FoodSon() s.bar('Hello') ``` FoodParent Son Hello FoodParent Son message 18. # 程式的開關 r 讀 報錯 不改變內容 w 寫 建立檔案 清空內容 a (append()) 建立檔案 不改變內容 r+ 讀寫 報錯 不改變內容 w+ 讀寫 建立檔案 清空內容 a+ 讀寫 建立檔案 不改變內容 ```python= fn='chll_15.txt' str1='I Love Python.' str2='Learn Python from the best book.' with open(fn,'w') as file_obj: file_obj.write(str1+'\n') file_obj.write(str2+'\n') ``` ```python= peom = 'There ws a young lady named Bright,Whose speed was far faster then light; She started on day In a relative way,And return on the previous night.' len(peom) fn='1234.txt' fout=open(fn,'wt') fout.write(peom) ``` ```python= #strip 去除空格 #str.strip()去除兩邊空格 #str.rstrip()去除字串右邊空格 #str.lstrip()去除字串左邊空格 dodo=' Hello Python ' dodo.strip() ``` 'Hello Python' ```python= dodo.rstrip() ``` ' Hello Python' ```python= dodo.lstrip() ``` 'Hello Python ' 如果用OPEN開檔案記得後面要加CLOSE把檔案關起來 不然會出現資源鎖定的問題 ```python= peom='There ws a young lady named Bright,Whose speed was far faster then light; She started on day In a relative way,And return on the previous night.' fn='relative.txt' fout=open(fn,'wt') fout.write(peom) fout.close() ``` 這樣就不會被鎖定了 但是用with open...as就不會有此問題 ```python= fn='relative.txt' with open(fn) as file_obj: obj_list=file_obj.readlines()#每次只讀一行 str_obj=''#空字串 for line in obj_list: str_obj+=line.rstrip() findstr=input('請輸入要搜尋的字串:') index=str_obj.find(findstr)#find只會第1字 print(type(index)) if index>=0: print('搜尋的%s字串在%s檔案中'%(findstr,fn)) print('在索引%s位置出現'%(index)) else: print('搜尋的%s字串不在%s檔案中'%(findstr,fn)) ``` 請輸入要搜尋的字串:y <class 'int'> ←這是指index裡面是以整數的形式存在 搜尋的y字串在relative.txt檔案中 在索引11位置出現 ```python= #建立資料夾 import os mydir='testch15' if os.path.exists(mydir):#檢查資料夾是否存在 print('%s資料夾存在'%(mydir)) else: os.mkdir(mydir)#建立資料夾 print('%s資料夾建立成功'%(mydir)) ``` testch15資料夾建立成功 ```python= #刪除資料夾 import os if os.path.exists(mydir):#檢查資料夾是否存在 os.rmdir(mydir)#刪除資料夾 print('%s資料夾刪除成功'%(mydir)) else: print('%s資料夾不存在'%(mydir)) ``` testch15資料夾刪除成功 ```python= import os mydir='testch14' if os.path.exists(mydir): print('%s資料夾存在'%(mydir)) else: os.mkdir(mydir) print('%s資料夾建立成功'%(mydir)) fn='testch14/123.txt' pome1=input('輸入日期:') pome2=input('輸入事件:') pome3=input('輸入心得:') with open(fn,'a') as file_obj: file_obj.write(pome1+'\n') file_obj.write(pome2+'\n') file_obj.write(pome3+'\n') with open(fn) as file_obj: obj_list=file_obj.readlines()#每次只讀一行 str_obj=''#空字串 for line in obj_list: str_obj+=line.rstrip() findstr=input('請輸入要搜尋的字串:') index=str_obj.find(findstr)#find只會第1字 if index>=0: print('搜尋的%s字串在%s檔案中'%(findstr,fn)) print('在索引%s位置出現'%(index)) else: print('搜尋的%s字串不在%s檔案中'%(findstr,fn)) ``` testch14資料夾存在 輸入日期:2020/01/02 輸入事件:吃火鍋 輸入心得:好冷 請輸入要搜尋的字串:火 搜尋的火字串在testch14/123.txt檔案中 在索引11位置出現 ```python= #二進位資料字入與讀 import pickle i=1300000 a=90.056 s='123abc' lst=[[1,2,3],[4,5,6],[7,8,9]] tu=(-5,10,8) coll={4,5,6} dic={'a':'apple','o':'orange','b':'banana'} data=[i,a,s,lst,tu,coll,dic] with open('sample_pickle.dat','wb') as f: try: pickle.dump(len(data),f)#壓縮 for item in data: pickle.dump(item,f) except: print('寫入文件異常') with open('sample_pickle.dat','rb') as f: n=pickle.load(f)#提取 for i in range(n): x=pickle.load(f) print(x) ``` 1300000 90.056 123abc [[1, 2, 3], [4, 5, 6], [7, 8, 9]] (-5, 10, 8) {4, 5, 6} {'a': 'apple', 'o': 'orange', 'b': 'banana'} pickle式python中保存壓縮提取文件的橫組,利用dump壓所並保存成字典的方式,透過load運行提取 由於在壓縮的過程中可能會出現錯誤 所以要用try去執行 排除錯誤 ## 用正規表達式來匹配 #compile() 編譯 #match() 匹配 #search() 會回傳第一個匹配項目 #findall() 會回傳所有不重疊的匹配 #split() 會用模式,在匹配處分割來源,並回傳一串字串片段 #sub() 會使用另一個取代引數,並將來源內,所有匹配模式的項目換成取代引數 #使用match()來取出匹配項目 #特殊字元 #\d 一個數字 #\D 一個非數字 #\w 一個英數字元 #\W 一個非英數字元 #\s 一個空白字元 #\S 一個非空白字元 #\b 一個單字範圍(介於 \w和\W,無論順序為何) #\B 一個非單字範圍 #使用指定符 #a | b 或 #. 除了\n之外的任何字元 #^來源字串的開頭 #$來源字串的結尾 #prev ? 零或一個prev #prev * 零或多個prev 越多越好 #prev *? 零或一個prev 越少越好 #prev + 一個或多個prev 越多越好 #prev +? 一個或多個prev 越少越好 #prev {m} m個連續的prev #prev {m , n} m到n個連續的prev 越多越好 #prev {m , n}? m到n個連續的prev 越少越好 #[ abc ] a或b或c #[ ^abc ] not(a或b或c) #prev (?= next) prev,如果它後面有next的話 #prev (?| next) prev,如果它後面沒有next的話 #(?<= prev) next 如果next之前是prev,匹配next #(?<! prev) next 如果next之前不是prev,匹配next ```python= #建立excel資料 import csv with open('menu.csv','w',encoding='utf-8-sig') as csvfile: fileobj=csv.writer(csvfile) fileobj.writerow(['品名','價格']) fileobj.writerow(['湯麵','50']) fileobj.writerow(['炒米粉','45']) fileobj.writerow(['蛋花湯','35']) fileobj.writerow(['乾麵','40']) ``` ```python= #建立excel資料 import csv with open('menu.csv','w',encoding='utf-8-sig',newline='') as csvfile: fileobj=csv.writer(csvfile) fileobj.writerow(['品名','價格']) fileobj.writerow(['湯麵','50']) fileobj.writerow(['炒米粉','45']) fileobj.writerow(['蛋花湯','35']) fileobj.writerow(['乾麵','40']) ``` ```python= #讀取excel檔 import csv with open('menu.csv','r',encoding='utf-8-sig') as f: reader=csv.reader(f) for row in reader: print(row[0],"",row[1]) ``` 品名 價格 湯麵 50 炒米粉 45 蛋花湯 35 乾麵 40 ```python= #建立txt檔案 fn='0107.txt' str1='Python好玩' str2='Python有趣' str3='Python好厲害' with open(fn,'wt') as file_obj: file_obj.write(str1+'\n') file_obj.write(str2+'\n') file_obj.write(str3+'\n') ``` ```python= #read() #一次取出整個檔案,處理大型檔案時要小心容量問題 p='' fin=open('0107.txt','rt') while True: fragement=fin.read() if not fragement:#因為read會回傳空字串 break p+=fragement print(p) ``` Python好玩 Python有趣 Python好厲害 ```python= #readline() #一次讀取一行,讀取時占用記憶體較小,比較適合大檔案 p='' fin=open('0107.txt','rt') while True: line=fin.readline() if not line: break p+=line print(p) ``` Python好玩 Python有趣 Python好厲害 ```python= #readlines() #每次只讀一行,回傳一個串列 p='' fin=open('0107.txt','rt') lines=fin.readlines() fin.close() print(len(lines),'lines read') print(lines)#lines是儲存成串列 for i in lines: p+=i.strip() print(p) ``` 3 lines read ['Python好玩\n', 'Python有趣\n', 'Python好厲害\n'] Python好玩 Python好玩Python有趣 Python好玩Python有趣Python好厲害 ```python= #readlines() #每次只讀一行,回傳一個串列 p='' fin=open('0107.txt','rt') lines=fin.readlines() fin.close() print(len(lines),'lines read') print(lines)#lines是儲存成串列 for i in lines: # p+=i.strip() print(i) ``` 3 lines read ['Python好玩\n', 'Python有趣\n', 'Python好厲害\n'] Python好玩 Python有趣 Python好厲害 ```python= #在mode中加入'b'就會以二進位模式開啟,會讀取與字入byte,而不是字串 bdata=bytes(range(0,256)) print(len(bdata)) print(bdata) ``` 256 b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff' ```python= import csv villains=[['Doctor','No'],['Rosa','Klebb'],['Mister','Big'],['Auric','Goldfinger'],['Ernst','Blofeld']] with open('abc.csv','w') as fout: csvout=csv.writer(fout) csvout.writerows(villains) #接著讀回字入的資料 with open('abc.csv','r') as fin: cin=csv.reader(fin) v=[row for row in cin]#使用串列生成式 print(v) ``` [['Doctor', 'No'], [], ['Rosa', 'Klebb'], [], ['Mister', 'Big'], [], ['Auric', 'Goldfinger'], [], ['Ernst', 'Blofeld'], []] ```python= import csv villains=[['Doctor','No'],['Rosa','Klebb'],['Mister','Big'],['Auric','Goldfinger'],['Ernst','Blofeld']] with open('abc.csv','w',newline='') as fout: csvout=csv.writer(fout) csvout.writerows(villains) #接著讀回字入的資料 with open('abc.csv','r') as fin: cin=csv.reader(fin) v=[row for row in cin]#使用串列生成式 print(v) ``` [['Doctor', 'No'], ['Rosa', 'Klebb'], ['Mister', 'Big'], ['Auric', 'Goldfinger'], ['Ernst', 'Blofeld']] ```python= #DictReader()讀取會是字典格式,並指定欄位名稱 import csv with open('abc.csv','r') as fin: cin=csv.DictReader(fin,fieldnames=['first','last']) vi=[row for row in cin] vi ``` [OrderedDict([('first', 'Doctor'), ('last', 'No')]), OrderedDict([('first', 'Rosa'), ('last', 'Klebb')]), OrderedDict([('first', 'Mister'), ('last', 'Big')]), OrderedDict([('first', 'Auric'), ('last', 'Goldfinger')]), OrderedDict([('first', 'Ernst'), ('last', 'Blofeld')])] ```python= import csv villains=[{'last':'No','first':'Doctor'},{'last':'Klebb','first':'Rosa'},{'last':'Big','first':'Mister'},{'last':'Goldfinger','first':'Auric'},{'last':'Blofeld','first':'Ernst'}] with open('villains.csv','wt',newline='') as fin: cout=csv.DictWriter(fin,['first','last']) cout.writeheader() cout.writerows(villains) #直接開檔案看結果 ``` # 19.正則表達式進行批配 1.需import re模組 2.compile()進行編譯 3.match()匹配 4.serch()回傳第一個批配到的結果 #很像find 5.findall()回傳所有不重疊的結果 6.split()會用模式在匹配處分割來源 7.sub()會使用另一個取代引數,將所有匹配的項目取代 ```python= #Python String下的printable有100個ASCII字元 import string printable=string.printable print(len(printable)) print(printable[0:50])#找出50個 ``` 100 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN ```python= #有哪些是數字 import re a=re.findall('\d',printable) print(a) ``` ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] ```python= #有哪些字原式數字,字母,或底線 import re a=re.findall('\w',printable) print(a) ``` ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_'] ```python= #有哪些是空白 import re a=re.findall('\s',printable) print(a) ``` [' ', '\t', '\n', '\r', '\x0b', '\x0c'] ```python= import re source='Young Frankn' m=re.match('You',source) print(m.group()) n=re.match('.*Frank',source) print(n.group())#.代表任何東西,*代表數量 ``` You Young Frank ```python= #search()找出Frank import re a=re.search('Frank',source) print(a.group()) ``` Frank ```python= #findall找出所有的n import re b=re.findall('n',source) print(b) ``` ['n', 'n', 'n'] ```python= #找出n後面有字元的 import re c=re.findall('n\S',source)#還有split n. print(c) ``` ['ng', 'nk'] ```python= #使用split()碰到n就分割 import re d=re.split('n',source) print(d) ``` ['You', 'g Fra', 'k', ''] ```python= #使用sub()把n換成? import re e=re.sub('n','?',source) print(e) ``` You?g Fra?k? ```python= #找出n後面有字元的 import re f=re.findall('n.?',source) print(f) ``` ['ng', 'nk', 'n'] ```python= import re source = 'I wish I may,I wish I might ...Have a dish of fish tonight...' a=re.findall('wish',source) print(a) b=re.findall('wish|fish',source) print(b) c=re.findall('^wish',source) print(c) d=re.findall('^I wish',source) print(d) e=re.findall('fish$',source) print(e) f=re.findall('fish tonight...$',source) print(f) g=re.findall('[wf]ish',source) print(g) h=re.findall('[wsh]+',source) print(g) i=re.findall('ght\W',source) print(i) ``` ['wish', 'wish'] ['wish', 'wish', 'fish'] [] ['I wish'] [] ['fish tonight...'] ['wish', 'wish', 'fish'] ['wish', 'wish', 'fish'] ['ght ', 'ght.'] ```python= import re msg1='Please call my secretary using 0939-030-111 or 0952-001-001' msg2='請明天5:30和我一起參加晚宴' msg3='請明天5:30和我一起參加晚宴,可用0933-080-080連絡我' def parsestring(string): phoneRule=re.compile(r'\d\d\d\d-\d\d\d-\d\d\d')#r是不轉意 phoneNum=phoneRule.search(string) if phoneNum!=None: print('電話號碼是%s'%phoneNum.group()) else: print('%s字串沒有電話號碼'%string) parsestring(msg1) parsestring(msg2) parsestring(msg3) ``` 電話號碼是0939-030-111 請明天5:30和我一起參加晚宴字串沒有電話號碼 電話號碼是0933-080-080 ```python= #改成findall import re msg1='Please call my secretary using 0939-030-111 or 0952-001-001' msg2='請明天5:30和我一起參加晚宴' msg3='請明天5:30和我一起參加晚宴,可用0933-080-080連絡我' def parsestring(string): phoneRule=re.compile(r'\d\d\d\d-\d\d\d-\d\d\d')#r是不轉意 phoneNum=phoneRule.findall(string) if phoneNum!=None: print('電話號碼是%s'%phoneNum) else: print('%s字串沒有電話號碼'%string) parsestring(msg1) parsestring(msg2) parsestring(msg3) ``` 電話號碼是['0939-030-111', '0952-001-001'] 電話號碼是[] 電話號碼是['0933-080-080'] ```python= import re msg = 'John and Tom will attend my party tonight. John is my best friend.' a=re.findall('John|Tom',msg) print(a) b=re.findall('Mary|Tom',msg) print(b) ``` ['John', 'Tom', 'John'] ['Tom'] group()是指正則表達式是由?組組成 例如group(1)是指第一組子串,group(2)是指第二組子串,groups()是所有子串組成的集合 ```python= import re a='123abc456' m=re.match('([0-9]*)([a-z]*)([0-9]*)',a) print(m.group()) print(m.group(0)) print(m.group(1)) print(m.group(2)) print(m.group(3)) ``` 123abc456 123abc456 123 abc 456 ```python=import re line="Cats are smarter than dogs" matchobj=re.match(r'(.*) are (.*) (.*) (.*)',line) if matchobj: print('matchobj.group():',matchobj.group()) print('matchobj.group(1):',matchobj.group(1)) print('matchobj.group(2):',matchobj.group(2)) print('matchobj.group(3):',matchobj.group(3)) print('matchobj.group(4):',matchobj.group(4)) else: print('No match') ``` matchobj.group(): Cats are smarter than dogs matchobj.group(1): Cats matchobj.group(2): smarter matchobj.group(3): than matchobj.group(4): dogs ```python= msg='Please call my secretary using 02-26999999' pattern=r'(\d{2})-(\d{8})' #pattern= phoneNum=re.search(pattern,msg) areaNum,localNum=phoneNum.groups() print('區域號碼是%s'%areaNum) print('電話號碼是%s'%localNum) print('完整號碼是%s'%phoneNum.group()) print('區域號碼是%s'%phoneNum.group(1)) print('電話號碼是%s'%phoneNum.group(2)) ``` 區域號碼是02 電話號碼是26999999 完整號碼是02-26999999 區域號碼是02 電話號碼是26999999 ```python= msg='Please call my secretary using (02)-26999999' #pattern=r'(\d{2})-(\d{8})' pattern=r'(\(\d{2}\))-(\d{8})' phoneNum=re.search(pattern,msg) areaNum,localNum=phoneNum.groups() print('區域號碼是%s'%areaNum) print('電話號碼是%s'%localNum) print('完整號碼是%s'%phoneNum.group()) print('區域號碼是%s'%phoneNum.group(1)) print('電話號碼是%s'%phoneNum.group(2)) ``` 區域號碼是(02) 電話號碼是26999999 完整號碼是(02)-26999999 區域號碼是(02) 電話號碼是26999999 enumerate 會跌代串列,字典等 (VS CODE) ```python= import numpy as np np.random.seed(0)#指定一個種子值確保每一次執行時均會產生相通的亂數內容 ``` ```python= import numpy as np np.array([1,4,2,5,3]) ``` array([1, 4, 2, 5, 3]) ```python= #浮點數型態,numpy只接受相同資料型態 np.array([3.14,4,2,3]) ``` array([3.14, 4. , 2. , 3. ]) ```python= #需要明確的宣告資料型態,可用dtype np.array([1,3,4,5,6],dtype='float32') ``` array([1., 3., 4., 5., 6.], dtype=float32) ```python= #巢狀list產生多維陣列 np.array([range(i,i+3)for i in [2,4,6]]) ``` array([[2, 3, 4], [4, 5, 6], [6, 7, 8]]) ```python= import numpy as np np.random.seed(0) x1=np.random.randint(10,size=6) x2=np.random.randint(10,size=(3,4)) x3=np.random.randint(10,size=(3,4,5)) print('x3 ndim=',x3.ndim)#維度的數量 print('x3 shape=',x3.shape)#每個維度的大小 print('x3 size=',x3.size)#整個陣列的總大小 3*4*5=60 ``` x3 ndim= 3 x3 shape= (3, 4, 5) x3 size= 60 ```python= print('itemsize:',x3.itemsize,'bytes')#陣列中元素的大小(byte為單位) print('nbytes:',x3.nbytes,'bytes')#列出整個陣列的大小(byte為單位) 3*4*5*8 ``` itemsize: 4 bytes nbytes: 240 bytes ```python= x2[:2,:3]#多維陣列的切片 ``` array([[3, 5, 2], [7, 6, 8]]) ```python= x2[:3,::2]#切偶數欄 ``` array([[3, 2], [7, 8], [1, 7]]) ```python= print(x2[::-1,::-1])#子陣列所有維一起反轉 print(x2[:,0])#取出第一欄 print(x2[0,:])#取出第一列 ``` [[7 7 6 1] [8 8 6 7] [4 2 5 3]] [3 7 1] [3 5 2 4] ```python= #陣列的複製 x2_sub_copy=x2[:2,:2].copy() print(x2_sub_copy) x2_sub_copy[0][0]=42 print(x2_sub_copy) print(x2) ``` [[3 5] [7 6]] [[42 5] [ 7 6]] [[3 5 2 4] [7 6 8 8] [1 6 7 7]] ```python= #reshape陣列的重塑 #重塑後的大小需和重塑前相等,否則無法執行重塑 import numpy as np grid=np.arange(1,10).reshape((3,3)) print(grid) ``` [[1 2 3] [4 5 6] [7 8 9]] ```python= z=np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]) print(z.shape) print(z.reshape(1,16)) print(z.reshape(16,1)) print(z.reshape(2,8)) print(z.reshape(8,2)) print(z.reshape(4,4)) ``` (4, 4) [[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]] [[ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16]] [[ 1 2 3 4 5 6 7 8] [ 9 10 11 12 13 14 15 16]] [[ 1 2] [ 3 4] [ 5 6] [ 7 8] [ 9 10] [11 12] [13 14] [15 16]] [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] ```python= #不知道陣列的屬性是多少,但是想把它變成一列,行數不知道多少 #可透過reshape(-1,1),numpy會自動幫你計算 z.reshape(-1,1) #你也可以這麼做 z.reshape(-1,2)#等同於(8,2) print(z.reshape(-1,1)) print(z.reshape(-1,2)) ``` [[ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16]] [[ 1 2] [ 3 4] [ 5 6] [ 7 8] [ 9 10] [11 12] [13 14] [15 16]] ```python= #np.concatenate串接陣列 x=np.array([1,2,3]) y=np.array([3,2,1]) np.concatenate([x,y]) ``` array([1, 2, 3, 3, 2, 1]) ```python= #一次串接兩個以上 z=[99,99,99] np.concatenate([x,y,z]) ``` array([ 1, 2, 3, 3, 2, 1, 99, 99, 99]) ```python= grid=np.array([[1,2,3], [4,5,6]]) #沿著第一軸串接 print(np.concatenate([grid,grid])) print(np.concatenate([grid,grid],axis=1)) ``` [[1 2 3] [4 5 6] [1 2 3] [4 5 6]] [[1 2 3 1 2 3] [4 5 6 4 5 6]] ```python= #不同維度的串接np.vstack垂直堆疊,np.hstack水瓶堆疊 #一定要是同列數或同欄數 import numpy as np x=np.array([1,2,3]) y=np.array([[9,8,7], [6,5,4]]) #vstack array print(np.vstack([x,y])) #hstack array grid=np.array([[99],[99]]) print(np.hstack([y,grid])) ``` [[1 2 3] [9 8 7] [6 5 4]] [[ 9 8 7 99] [ 6 5 4 99]] ```python= import numpy as np a=[1,2,3,99,99,3,2,1] b=[1,2,3,99,99,3,2,1,5] c=[[1,2,3],[1,2,3]] d=[1,2,3] print(np.concatenate([a,b])) print(np.vstack([c,d])) print(np.hstack([a,b])) ``` [ 1 2 3 99 99 3 2 1 1 2 3 99 99 3 2 1 5] [[1 2 3] [1 2 3] [1 2 3]] [ 1 2 3 99 99 3 2 1 1 2 3 99 99 3 2 1 5] ```python= #串接 #產生1~50之間奇數5*5矩陣 import numpy as np a=np.arange(1,51,2).reshape((5,5))#(5,-1)也行 #產生1~50之間偶數5*5矩陣 b=np.arange(2,51,2).reshape((5,5))#(5,-1)也行 #將a和b做垂直堆疊 v_stack=np.vstack([a,b]) #將a和b作水平堆疊 h_stack=np.hstack([a,b]) #顯示結果 print('垂直堆疊\n',v_stack) print('水平堆疊\n',h_stack) ``` 垂直堆疊 [[ 1 3 5 7 9] [11 13 15 17 19] [21 23 25 27 29] [31 33 35 37 39] [41 43 45 47 49] [ 2 4 6 8 10] [12 14 16 18 20] [22 24 26 28 30] [32 34 36 38 40] [42 44 46 48 50]] 水平堆疊 [[ 1 3 5 7 9 2 4 6 8 10] [11 13 15 17 19 12 14 16 18 20] [21 23 25 27 29 22 24 26 28 30] [31 33 35 37 39 32 34 36 38 40] [41 43 45 47 49 42 44 46 48 50]] ```python= #切片 import numpy as np arr=np.arange(12).reshape((3,4)) print('array is:') print(arr) #取第一維索引1到索引12之間的元素 #取第二維索引1到索引12之間的元素 slice_one=arr[1:2,1:3] print('first slice is:') print(slice_one) ``` array is: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] first slice is: [[5 6]] repeat函數 對元素進行複製 numpy.repeat(a,repeats,axis=None) a→陣列 repeats→重複的次數 axis=None→陣列的維度 ```python= import numpy as np a=np.arange(10) a a.repeat(5) np.zeros(10,dtype=int) ``` array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) ```python= np.ones((3,5),dtype='float') ``` array([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]) ```python= np.full((3,5),3.14) ``` array([[3.14, 3.14, 3.14, 3.14, 3.14], [3.14, 3.14, 3.14, 3.14, 3.14], [3.14, 3.14, 3.14, 3.14, 3.14]]) np.random.normal(平均值,標準差,大小(n,n)) normal一般數值 randint整數 ```python= #numpu ufunc 計算式 x=np.arange(4) print('x =',x) print('x+5=',x+5) #np.add print('x-5=',x-5) #np.subtract print('x*5=',x*5) #np.multiply print('x/5=',x/5) #np.divide print('x%5=',x%5) #np.mod print('x//5=',x//5) #np.floor print('-x=',-x) print('x**2=',x**2) #np.power ``` x = [0 1 2 3] x+5= [5 6 7 8] x-5= [-5 -4 -3 -2] x*5= [ 0 5 10 15] x/5= [0. 0.2 0.4 0.6] x%5= [0 1 2 3] x//5= [0 0 0 0] -x= [ 0 -1 -2 -3] x**2= [0 1 4 9] ```python= #三角函數 theta=np.linspace(0,np.pi,3) print('theta =',theta) print('sin(theta)=',np.sin(theta)) print('cos(theta)=',np.cos(theta)) print('tan(theta)=',np.tan(theta)) ``` theta = [0. 1.57079633 3.14159265] sin(theta)= [0.0000000e+00 1.0000000e+00 1.2246468e-16] cos(theta)= [ 1.000000e+00 6.123234e-17 -1.000000e+00] tan(theta)= [ 0.00000000e+00 1.63312394e+16 -1.22464680e-16] ```python= #反三角函數 x=[-1,0,1] print('x =',x) print('arcsin(x)=',np.arcsin(x)) print('arccos(x)=',np.arccos(x)) print('arctan(x)=',np.arctan(x)) ``` x = [-1, 0, 1] arcsin(x)= [-1.57079633 0. 1.57079633] arccos(x)= [3.14159265 1.57079633 0. ] arctan(x)= [-0.78539816 0. 0.78539816] #np.sum 加總 #np.prod 乘積 #np.mean 平均值 #np.std 標準差 #np.var 變異量 #np.min 最小值 #np.max 最大值 #np.argmin 最小值的索引 #np.argmax 最大值的索引 #np.median 中位數 #np.percentile 排名統計 #np.any 當陣列中有任一值是True或是非零值時傳回True #np.all 當陣列中有所有值是True或是非零值時傳回True ```python= import numpy as np import pandas as pd data=pd.read_csv('president_heights.csv') height=np.array(data['height(cm)']) print(height) print('身高平均值:',height.mean()) print('標準差:',np.std(height)) print('最高的身高為:',np.max(height)) print('最矮的身高為:',np.min(height)) print('中位數:',np.median(height)) ``` [189 170 189 163 183 171 185 168 173 183 173 173 175 178 183 193 178 173 174 183 183 168 170 178 182 180 183 178 182 188 175 179 183 193 182 183 177 185 188 188 182 185] 身高平均值: 179.73809523809524 標準差: 6.931843442745892 最高的身高為: 193 最矮的身高為: 163 中位數: 182.0 # 陣列相加 a+b 當不同維度的需進行運算時,需符合以下規則 規則1.兩個陣列尺寸不同,尺寸較小的會在其前(左)側填充 規則2.兩個陣列在任何形狀上都不匹配時,則會進行拉伸 規則3.大小不一致也不等於1時則會發生錯誤 ```python= #example 1 #Broodcasting import numpy as np M=np.ones((2,3)) a=np.arange(3) print(M) print(a) M+a ``` [[1. 1. 1.] [1. 1. 1.]] [0 1 2] array([[1., 2., 3.], [1., 2., 3.]]) ```python= #example 2 import numpy as np a=np.arange(3).reshape(3,1) b=np.arange(3) print(a) print(b) a+b ``` [[0] [1] [2]] [0 1 2] array([[0, 1, 2], [1, 2, 3], [2, 3, 4]]) ```python= import numpy as np rand=np.random.RandomState(42) x=rand.randint(100,size=10) print(x) ``` [51 92 14 71 60 20 82 86 74 74] ```python= #假設我們想要印出其中三個數可以這樣做 x[3],x[7],x[2] ``` (71, 86, 14) ```python= #或著我們可以放進串列也可以得到相同的結果 ind=[3,7,4] x[ind] ``` array([71, 86, 60]) ```python= #numpy.random.RandomState()跟seed種子雷同 import numpy as np for i in [1,2,3,4]: rng=np.random.RandomState(2) arrayA=rng.uniform(0,1,(2,3))#2列3欄每個元素都是在0,1之間 print('i=%s'%i) print(arrayA) ``` i=1 [[0.4359949 0.02592623 0.54966248] [0.43532239 0.4203678 0.33033482]] i=2 [[0.4359949 0.02592623 0.54966248] [0.43532239 0.4203678 0.33033482]] i=3 [[0.4359949 0.02592623 0.54966248] [0.43532239 0.4203678 0.33033482]] i=4 [[0.4359949 0.02592623 0.54966248] [0.43532239 0.4203678 0.33033482]] ```python= import numpy as np for i in [1,2,3,4]: rng=np.random.RandomState(2+i) arrayA=rng.uniform(0,1,(2,3)) print('i=%s'%i) print(arrayA) ``` i=1 [[0.5507979 0.70814782 0.29090474] [0.51082761 0.89294695 0.89629309]] i=2 [[0.96702984 0.54723225 0.97268436] [0.71481599 0.69772882 0.2160895 ]] i=3 [[0.22199317 0.87073231 0.20671916] [0.91861091 0.48841119 0.61174386]] i=4 [[0.89286015 0.33197981 0.82122912] [0.04169663 0.10765668 0.59505206]] ```python= #遮罩 import numpy as np import pandas as pd rng=np.random.RandomState(0) x=rng.randint(10,size=(3,4)) x ``` array([[5, 0, 3, 3], [7, 9, 3, 5], [2, 4, 7, 6]]) ```python= x<6 ``` array([[ True, True, True, True], [False, False, True, True], [ True, True, False, False]]) ```python= x[x<5] ``` array([0, 3, 3, 3, 2, 4]) ```python= np.sum(x<5,axis=1)#橫著加 ``` array([3, 1, 2]) ```python= np.sum(x<5,axis=0)#直著加 ``` array([1, 2, 2, 1]) ```python= np.any(x>8) ``` True ```python= np.all(x>8) ``` False ```python= import numpy as np import pandas as pd rainfall=pd.read_csv('Seattle2014.csv')['PRCP'].values inches=rainfall/254.0 inches.shape print('沒有下雨的天數:',np.sum(inches==0)) print('下雨的天數:',np.sum(inches!=0)) print('雨量超過0.5inches的天數:',np.sum(inches>0.5)) print('雨量小於0.2inches的天數:',np.sum((inches<0.2)&(inches>0))) #對於所有的雨天資料建立一個遮罩 rainy=(inches>0) #建立一個所有夏天(6/21是第172天到第262天)的遮罩 days=np.arange(365) summer=(days>=172)&(days<=262) print('2014年雨量的中位數是:',np.median(inches[rainy])) print('2014年夏天降雨量的中位數:',np.median(inches[summer])) ``` 沒有下雨的天數: 215 下雨的天數: 150 雨量超過0.5inches的天數: 37 雨量小於0.2inches的天數: 75 2014年雨量的中位數是: 0.19488188976377951 2014年夏天降雨量的中位數: 0.0 # 矩陣相乘 ```python= import numpy as np import pandas as pd A=np.array([[1,2,3],[4,5,6]]) print(A.shape) B=np.array([[1,2],[3,4],[5,6]]) print(B.shape) np.dot(A,B) ``` (2, 3) (3, 2) array([[22, 28], [49, 64]]) # Pandas 資料結構 1.Series:用來處理時間序列相關資料(如感測器資料),主要為一維陣列 2.DataFrame:處理結構化資料,有列和欄的二維資料索引,例如關練是資料庫.csv 3.Panel:處理資料及索引,列索引和欄標籤的三為資料集 #資料描述查詢 shape 回傳列數與欄數 describe() 回傳描述性統計 head(int) 回傳前N筆觀測值 tail(int) 回傳從N筆觀測值 columns 回傳欄位名稱 index 回傳index info() 回傳資料內容 ```python= import numpy as np import pandas as pd data=pd.Series([0.25,0.5,0.75,1]) data ``` 0 0.25 1 0.50 2 0.75 3 1.00 dtype: float64 ```python= s=pd.Series([1,3,5,np.nan,6,8]) s ``` 0 1.0 1 3.0 2 5.0 3 NaN 4 6.0 5 8.0 dtype: float64 ```python= #pandas可以透過values和index屬性取值 data.values ``` array([0.25, 0.5 , 0.75, 1. ]) ```python= s.values ``` array([ 1., 3., 5., nan, 6., 8.]) ```python= data.index ``` RangeIndex(start=0, stop=4, step=1) ```python= s.index ``` RangeIndex(start=0, stop=6, step=1) ```python= #Series 可以使用字串作為索引 data=pd.Series([0.25,0.5,0.75,1],index=['a','b','c','d']) data ``` a 0.25 b 0.50 c 0.75 d 1.00 dtype: float64 ```python= #索引'b' data['b'] ``` 0.5 ```python= #pandas也可以使用字典的方式去構造索引 population_dict={'California':3833256,'Texas':19651127,'Florida':19552860,'Illinois':12882135} population=pd.Series(population_dict)#對應字典型態資料結構 population ``` California 3833256 Texas 19651127 Florida 19552860 Illinois 12882135 dtype: int64 ```python= population['California'] ``` 3833256 ```python= population['California':'Illinois'] ``` California 3833256 Texas 19651127 Florida 19552860 Illinois 12882135 dtype: int64 ```python= #索引可以是整數序列 pd.Series(5,index=[100,200,300]) ``` 100 5 200 5 300 5 dtype: int64 ```python= #索引也可以是字典 pd.Series({2:'a',1:'b',3:'c'}) ``` 2 a 1 b 3 c dtype: object ```python= import pandas as pd dates=pd.date_range('20190101',periods=12) print(dates) ``` DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08', '2019-01-09', '2019-01-10', '2019-01-11', '2019-01-12'], dtype='datetime64[ns]', freq='D') ```python= date=pd.Series(dates) date ``` 0 2019-01-01 1 2019-01-02 2 2019-01-03 3 2019-01-04 4 2019-01-05 5 2019-01-06 6 2019-01-07 7 2019-01-08 8 2019-01-09 9 2019-01-10 10 2019-01-11 11 2019-01-12 dtype: datetime64[ns] ```python= status=pd.DataFrame({'columns':dates,'rows':date}) print(status) ``` columns rows 0 2019-01-01 2019-01-01 1 2019-01-02 2019-01-02 2 2019-01-03 2019-01-03 3 2019-01-04 2019-01-04 4 2019-01-05 2019-01-05 5 2019-01-06 2019-01-06 6 2019-01-07 2019-01-07 7 2019-01-08 2019-01-08 8 2019-01-09 2019-01-09 9 2019-01-10 2019-01-10 10 2019-01-11 2019-01-11 11 2019-01-12 2019-01-12 ```python= import pandas as pd dict_a={'a':1,'b':3,'c':5,'d':7,'e':9} dict_b={'a':2,'b':4,'c':6,'d':8,'e':10} s_a=pd.Series(dict_a) s_b=pd.Series(dict_b) print('\r\n print s_a\r\n{}'.format(s_a)) print('\r\n print s_b\r\n{}'.format(s_b)) ``` print s_a a 1 b 3 c 5 d 7 e 9 dtype: int64 print s_b a 2 b 4 c 6 d 8 e 10 dtype: int64 ```python= status=pd.DataFrame({'單數':s_a,'雙數':s_b}) print('\r\n print status\r\n{}'.format(status)) ``` print status 單數 雙數 a 1 2 b 3 4 c 5 6 d 7 8 e 9 10 ```python= import numpy as np import pandas as pd df=pd.read_csv('file1.csv') #改變索引從1開始並顯示結果 df=df.set_index(np.arange(1,len(df)+1)) print(df) #顯示前兩筆記錄 print('\n顯示前兩筆記錄:\n%s'%df[0:2]) #印出"白醋"的進貨紀錄 print('\n"白醋的進貨紀錄"\n%s'%df[df['商品名稱']=='白醋']) #印出進貨日期2018/5/9 print('\n"印出進貨日期2018/5/9"\n%s'%df[df['進貨日期']=='2018/5/9']) #計算各種單位商品的數量 print('\n各種單位商品的數量:\n%s'%df.groupby('單位')['商品名稱'].count()) #根據進貨日期從小到大排序 print('\n根據進貨日期從小到大排序:\n%s'%df.sort_values(by='進貨日期')) ``` 商品名稱 進貨日期 數量 單位 1 鹽巴 2018/5/2 15 公斤 2 白醋 2018/5/2 10 公升 3 醬油 2018/5/9 15 公升 4 胡椒粉 2018/5/10 12 包 5 辣椒粉 2018/4/30 8 包 顯示前兩筆記錄: 商品名稱 進貨日期 數量 單位 1 鹽巴 2018/5/2 15 公斤 2 白醋 2018/5/2 10 公升 "白醋的進貨紀錄" 商品名稱 進貨日期 數量 單位 2 白醋 2018/5/2 10 公升 "印出進貨日期2018/5/9" 商品名稱 進貨日期 數量 單位 3 醬油 2018/5/9 15 公升 各種單位商品的數量: 單位 公升 2 公斤 1 包 2 Name: 商品名稱, dtype: int64 根據進貨日期從小到大排序: 商品名稱 進貨日期 數量 單位 5 辣椒粉 2018/4/30 8 包 4 胡椒粉 2018/5/10 12 包 1 鹽巴 2018/5/2 15 公斤 2 白醋 2018/5/2 10 公升 3 醬油 2018/5/9 15 公升 ```python= import numpy as np import pandas as pd #商品名稱 products=['iPhone','Samsung','Sony','Huawei','HTC','Oppo'] #存貨量 inventory=[1000,1200,1300,800,700,850] #建立字典(商品名稱,存貨) df=pd.DataFrame({'商品名稱':products,'存貨量':inventory}) print(df) #將iPhone的存貨+50 df.loc[0,'存貨量']+=50 #印出存貨量最多的商品 print('\n 印出存貨量最多的商品:\n%s'%df[df['存貨量']==df['存貨量'].max()]) #印出存貨量最少的商品 print('\n 印出存貨量最少的商品:\n%s'%df[df['存貨量']==df['存貨量'].min()]) #將資料根據從小到大排序 print('\n將資料根據從小到大排序\n%s'%df.sort_values(by='存貨量')) #從大到小排序 print('\n從大到小排序\n%s'%df.sort_values(by='存貨量',ascending=False)) #存貨量少於1000的商品 print('\n 存貨量少於1000的商品:\n%s'%df[df['存貨量']<1000]) ``` 商品名稱 存貨量 0 iPhone 1000 1 Samsung 1200 2 Sony 1300 3 Huawei 800 4 HTC 700 5 Oppo 850 印出存貨量最多的商品: 商品名稱 存貨量 2 Sony 1300 印出存貨量最少的商品: 商品名稱 存貨量 4 HTC 700 將資料根據從小到大排序 商品名稱 存貨量 4 HTC 700 3 Huawei 800 5 Oppo 850 0 iPhone 1050 1 Samsung 1200 2 Sony 1300 從大到小排序 商品名稱 存貨量 2 Sony 1300 1 Samsung 1200 0 iPhone 1050 5 Oppo 850 3 Huawei 800 4 HTC 700 存貨量少於1000的商品: 商品名稱 存貨量 3 Huawei 800 4 HTC 700 5 Oppo 850 ```python= import numpy as np import pandas as pd #建立名單_學生名字 names=['Cathy','Dassy','Bonnie','Vicky','Mary'] #建立名單_學生分數 scores=[84,98,92,50,75] df=pd.DataFrame({'名字':names,'分數':scores}) print('原始資料:') print(df) #把Vicky的分數加30 df.loc[3,'分數']+=30 print('加30後:') print(df) #名字從小到大排序 print('\n 名字從小到大排序:\n%s'%df.sort_values(by='名字')) #分數從小到大排序 print('\n 分數從小到大排序:\n%s'%df.sort_values(by='分數',ascending=False)) #列出分數高於90分的學生 print('\n 列出分數高於90分的學生:\n%s'%df[df['分數']>90]) #列出分數低於80分的學生 print('\n 列出分數低於80分的學生:\n%s'%df[df['分數']<80]) ``` 原始資料: 名字 分數 0 Cathy 84 1 Dassy 98 2 Bonnie 92 3 Vicky 50 4 Mary 75 加30後: 名字 分數 0 Cathy 84 1 Dassy 98 2 Bonnie 92 3 Vicky 80 4 Mary 75 名字從小到大排序: 名字 分數 2 Bonnie 92 0 Cathy 84 1 Dassy 98 4 Mary 75 3 Vicky 80 分數從小到大排序: 名字 分數 1 Dassy 98 2 Bonnie 92 0 Cathy 84 3 Vicky 80 4 Mary 75 列出分數高於90分的學生: 名字 分數 1 Dassy 98 2 Bonnie 92 列出分數低於80分的學生: 名字 分數 4 Mary 75 ```python= import numpy as np import pandas as pd bmi_data={'Name':['Ron','Jerry','Nick','Andy','Masour'],'Height':[173,175,173,178,174],'Weight':[60,95,70,55,58]} df=pd.DataFrame(bmi_data,columns=bmi_data.keys()) print('原始資料\n',df) print() #給每一比原始資料計算BMI BMI=(df['Weight']/(df['Height']/100)**2).map('{:,.2f}'.format) #map是做資料型態的轉換 #獲得每一筆BMI狀態 state=[] for bmi in BMI.values: bmi=eval(bmi) if bmi<18.5: state.append('過輕') elif 18.5<=bmi<24: state.append('正常') elif 24<=bmi<27: state.append('過重') else: state.append('肥胖') BMI_df=pd.DataFrame({'BMI':BMI,'State':state}) #將BMI資料加入原始資料 df=df.join(BMI_df) print('加入BMI從\n',df) print('肥胖者:\n%s'%df[df['State']=='肥胖']) ``` 原始資料 Name Height Weight 0 Ron 173 60 1 Jerry 175 95 2 Nick 173 70 3 Andy 178 55 4 Masour 174 58 加入BMI從 Name Height Weight BMI State 0 Ron 173 60 20.05 正常 1 Jerry 175 95 31.02 肥胖 2 Nick 173 70 23.39 正常 3 Andy 178 55 17.36 過輕 4 Masour 174 58 19.16 正常 肥胖者: Name Height Weight BMI State 1 Jerry 175 95 31.02 肥胖 ```python= import pandas as pd a=pd.read_csv('examples\ex2.csv', header=None)#header是不指定標題 a ``` ![](https://i.imgur.com/kRT7c8C.jpg) ```python= b=pd.read_csv('examples\ex2.csv',names=['a','b','c','d','message'])#names是指定標題 b ``` ![](https://i.imgur.com/kaTQfLB.jpg) ```python= c=pd.read_csv('examples\ex2.csv',names=['a','b','c','d','message'],index_col='message')#index特別指定列索引名稱 c ``` ![](https://i.imgur.com/gNAUVBj.jpg) ```python= #顯示成層次化索引 d=pd.read_csv('examples\csv_mindex.csv',index_col=['key1','key2']) d ``` ![](https://i.imgur.com/iKeltyS.jpg) ```python= #找出遺漏值 e=pd.read_csv('examples\ex5.csv') e pd.isnull(e) ``` ![](https://i.imgur.com/cnnWw1r.jpg) ```python= #將遺漏值填進NULL f=pd.read_csv('examples\ex5.csv',na_values=['NULL']) f ``` ![](https://i.imgur.com/P3kzVrh.jpg) import **matplotlib.pyplot** as plt 繪圖用的套件 ```python= df=pd.read_csv('data\grades.csv') df ``` ![](https://i.imgur.com/FTUVSfz.jpg) ```python= df[:6] ``` ![](https://i.imgur.com/wgaKbRU.jpg) ```python= df['國文'] ``` 0 9 1 10 2 13 3 10 4 13 .. 95 9 96 8 97 14 98 15 99 9 Name: 國文, Length: 100, dtype: int64 ```python= #畫圖 df.國文.plot() ``` ![](https://i.imgur.com/x4Ohfpj.jpg) ```python= #平均 df.國文.mean() ``` 11.39 ```python= #標準差 df.國文.std() ``` 2.196852661459484 ```python= #全部都算... df.describe() ``` ![](https://i.imgur.com/K9Xkz1j.jpg) ```python= df['總級分']=df.sum(axis=1) ``` ```python= df.head() ``` ![](https://i.imgur.com/9GH6T35.jpg) ```python= df['加權']=df.國文+df.英文+df.數學*2 ``` ```python= df.head() ``` ![](https://i.imgur.com/994K72M.jpg) ```python= #小到大排列 df.sort_values(by="總級分").head(10) ``` ![](https://i.imgur.com/U1ycVNT.jpg) ```python= #由大到小排列 df.sort_values(by="總級分",ascending=False).head(10) ``` ![](https://i.imgur.com/leXUu8r.jpg) ```python= #由兩個條件去排列 df.sort_values(by=["總級分","加權"]).head(10) ``` ![](https://i.imgur.com/LoHGzMR.jpg) ```python= df2=df.sort_values(by=["總級分","加權"],ascending=False).head(10) df2 ``` ![](https://i.imgur.com/5C42FNh.jpg) ```python= #找出數學滿級分 df[df.數學==15] ``` ![](https://i.imgur.com/6ZkASHJ.jpg) ```python= #找出數學集英文滿級分 df[(df.數學==15)&(df.英文==15)] ``` ![](https://i.imgur.com/J9MuF90.jpg) ```python= #第83列刪掉了 df2.drop(83) ``` ![](https://i.imgur.com/Cenm5lT.jpg) ```python= import numpy as np import matplotlib.pyplot as plt x=np.linspace(-10,10,100)#從-10~10產生100個點 y=np.sin(x) plt.plot(x,y) ``` ![](https://i.imgur.com/Be7jomF.jpg) ```python= x=np.linspace(0,10,200) y=np.sin(5*x)/(1+x**2) plt.plot(x,y,'r') ``` ![](https://i.imgur.com/wTw7GXU.jpg) 'b'(藍色),'g'(綠色),'r'(紅色),'c'(青色),'m'(紫色),'y'(黃色),'k'(黑色),'w'(白色) 參數 -- dash -. 點+dash : 點點 。 大點點 ^ 三角 s 方塊 參數 alpha 透明度 color(c) 顏色 linestyle(ls) 線條風格 linewidth(lw) 線寬 marker mec(邊線的顏色) mew(邊線的寬度) mfc(marker的顏色) mfcalt(marker的替換色) ms(marker的大小) markevery(間隔多少畫一個marker) plt.title('標題') plt.xlabel('X軸名稱') plt.ylabel('Y軸名稱') ```python= x=range(20) y=np.random.randn(20) plt.plot(x,y) ``` ![](https://i.imgur.com/OS9rgmM.jpg) ```python= plt.plot(x,y,marker='o',mec='r',mfc='g',ms=10,lw=5) ``` ![](https://i.imgur.com/vSyONyH.jpg) ```python= #長條圖 plt.bar(range(1,6),np.random.randint(1,30,5)) ``` ![](https://i.imgur.com/T0hipIY.jpg) ```python= #雙色長條圖 x=np.arange(1,6) plt.bar(x-0.4,[3,10,8,12,6],width=0.4,ec='none',fc='#e63946') plt.bar(x,[6,3,12,5,8],width=0.4,ec='none',fc='#7fb069') ``` ![](https://i.imgur.com/2Q2nZKx.jpg) ```python= import matplotlib.pyplot as plt #公司各部門及員工數量 companyA={'Personnel Department':8, 'Accounting Department':5, 'Design Department':10, 'Marketing Department':15, 'Purchasing Department':5} companyB={'Personnel Department':10, 'Accounting Department':8, 'Design Department':20, 'Marketing Department':18, 'Purchasing Department':12} #A公司各部門員工數量 companyA_values=companyA.values() #B公司各部門員工數量 companyB_values=companyB.values() #各部門名稱 keys=companyA.keys() #各部門顏色 colors=['violet','yellow','skyblue','lightcoral','lightgreen'] #突顯行銷部門 explode=[0,0,0,0.1,0] #控制到圓心距離的 #數據顯示格式 autopct='%.1f%%' #起始角度 startangle=90 #繪出公司的圖形圖 fig1,ax1=plt.subplots() ax1.pie(companyA_values, labels=keys, explode=explode, colors=colors, autopct=autopct, startangle=startangle) ax1.axis('equal') #圖表標題 ax1.set_title('CompanyA',size=20) fig2,ax2=plt.subplots() ax2.pie(companyB_values, labels=keys, explode=explode, colors=colors, autopct=autopct, startangle=startangle) ax2.axis('equal') #圖表標題 ax2.set_title('CompanyB',size=20) plt.show() ``` ![](https://i.imgur.com/1wJKdFi.jpg) ```python= import csv import matplotlib.pyplot as plt city=[] population=[] with open('csvfile3.csv') as file: data = list(csv.reader(file)) for record in data[1:]: city.append(record[0]) population.append(record[1]) keys=city colors=['violet','yellow','skyblue','lightcoral','lightgreen','red'] explode=[0,0,0,0,0,0] autopct='%.1f%%' startangle=90 fig,ax=plt.subplots() ax.pie(population, labels=keys, explode=explode, colors=colors, autopct=autopct, startangle=startangle) ax.axis('equal') ax.set_title('City',size=20) plt.show() ``` ![](https://i.imgur.com/mpCtHJF.jpg) ## 合併 pandas的合併有append,merage,join,concat DataFrame.append(ignore_index=True) ```python= a.append({'numbers':100,'flots':5.75,'name':'Amy'},ignore_index=True) ``` ![](https://i.imgur.com/jwtlWtQ.jpg) ```python= import pandas as pd #concat合併,利用axis可設定垂直或水平合併 upper_df=pd.DataFrame() lower_df=pd.DataFrame() upper_df['char']=['Kitty','Monica','John'] upper_df['cast']=['Jennifer','Mary','Lisa'] lower_df['char']=['Joe','Ros','Jordan'] lower_df['cast']=['A','B','C'] print('upper_df') upper_df ``` ![](https://i.imgur.com/YM5azk9.jpg) ```python= import pandas as pd #concat合併,利用axis可設定垂直或水平合併 upper_df=pd.DataFrame() lower_df=pd.DataFrame() upper_df['char']=['Kitty','Monica','John'] upper_df['cast']=['Jennifer','Mary','Lisa'] lower_df['char']=['Joe','Ros','Jordan'] lower_df['cast']=['A','B','C'] print('lower_df') lower_df ``` ![](https://i.imgur.com/b37xHB0.jpg) ```python= pd.concat([upper_df,lower_df]) #沒有加上ignore_index時會索引不正確的問題 ``` ![](https://i.imgur.com/kp42vx3.jpg) ```python= #所以加上後 pd.concat([upper_df,lower_df],ignore_index=True) ``` ![](https://i.imgur.com/5T9aT1b.jpg) ## 連結 pd.merge() 高效能操作關聯式資料庫表格聯結和合併 有四種形式 one-to-one one-to-many many-to-one many-to-many ```python= #one-to-one import pandas as pd left_df=pd.DataFrame() right_df=pd.DataFrame() left_df['title']=['The Avengers','The Avengers: Age of Ultron','Avengers:Infinity','Avengers:Endgame'] left_df['releace_year']=[2012,2015,2018,2019] right_df['title']=['Avengers:Infinity','Avengers:Endgame','The Avengers','The Avengers: Age of Ultron'] right_df['rating']=[8.5,8.6,8.5,7.3] print(left_df) print(right_df) pd.merge(left_df,right_df) ``` ![](https://i.imgur.com/TEPVKC1.jpg) ```python= #one-to-many import pandas as pd left_df=pd.DataFrame() right_df=pd.DataFrame() left_df['title']=['The Avengers'] left_df['releace_year']=[2012] right_df['title']=['The Avengers','The Avengers','The Avengers'] right_df['genre']=['Action','Adventure','Sci-Fi'] print(left_df) print(right_df) pd.merge(left_df,right_df) ``` ![](https://i.imgur.com/xuYqzKZ.jpg) ```python= #merge 多對一 #劇情的類型,上映的年份,應用到電影名稱實現多對一聯結 import pandas as pd left_df=pd.DataFrame() right_df=pd.DataFrame() left_df['title']=['The Avengers','The Avengers','The Avengers'] left_df['genre']=['Action','Adventure','Sci-Fi'] right_df['title']=['The Avengers'] right_df['release_year']=[2012] print(left_df) print(right_df) pd.merge(left_df,right_df) ``` ![](https://i.imgur.com/0sGJHuw.jpg) ```python= import pandas as pd left_df=pd.DataFrame() right_df=pd.DataFrame() left_df['title']=['The Avengers','The Avengers: Age of Ultron','Avengers:Infinity','Avengers:Endgame'] left_df['releace_year']=[2012,2015,2018,2019] right_df['title']=['Avengers:Infinity','Avengers:Endgame','The Avengers','The Avengers: Age of Ultron'] right_df['rating']=[8.5,8.6,8.5,7.3] left_df=left_df.set_index('title') #將資料表設定某個相同的欄位做為列索引 set_index() right_df=right_df.set_index('title')#將資料表設定某個相同的欄位做為列索引 set_index() print(left_df) print(right_df) left_df.join(right_df) ``` ![](https://i.imgur.com/wojE74Z.jpg) ```python= import pandas as pd from string import ascii_uppercase import xlrd file_name='總統-A05-4-候選人得票數一覽表-各投開票所(臺北市).xls' xls_df=pd.read_excel(file_name,skiprows=[0,1,3,4]) column_names=list(xls_df.columns) n_cadidates=len(column_names)-11 cadidate_numbers_names=column_names[3:(3+n_cadidates)] column_names=['district','village','office']+cadidate_numbers_names+list(ascii_uppercase[:8]) xls_df.columns=column_names xls_df.head() ``` ![](https://i.imgur.com/FkPnOeG.jpg) strip() 去除兩邊的空白 rstrip() 去除右邊的空白 lstrip() 去除左邊的空白 ```python= xls_df['district']=xls_df['district'].str.replace('\u3000','').str.strip() print(xls_df['district'].unique()) xls_df.head() ``` ![](https://i.imgur.com/UJqtP8H.jpg) ```python= #捨去統計與小計觀測值 #傳用DataFrame的dropna() xls_df=xls_df.dropna().reset_index(drop=True)#reset_index(drop=True)跟ignore_index=True是相同的意思 xls_df.head() ``` ![](https://i.imgur.com/3fCcMKQ.jpg) ```python= #填補行政區 Series.fillna() imputed_district=xls_df['district'] imputed_district=imputed_district.fillna(method='ffill') xls_df=xls_df.drop('district',axis=1) xls_df.insert(0,'district',imputed_district) xls_df.head() ``` ![](https://i.imgur.com/Dp4AqLE.jpg) ```python= import pandas as pd import sys df1 = pd.DataFrame({ 'A': ['A0', 'A1', 'A2', 'A3'], 'B': ['B0', 'B1', 'B2', 'B3'], 'C': ['C0', 'C1', 'C2', 'C3'], 'D': ['D0', 'D1', 'D2', 'D3']}) df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'], 'B': ['B4', 'B5', 'B6', 'B7'], 'C': ['C4', 'C5', 'C6', 'C7'], 'D': ['D4', 'D5', 'D6', 'D7']}) df3 = pd.DataFrame({'A': ['A8', 'A9', 'A10', 'A11'], 'B': ['B8', 'B9', 'B10', 'B11'], 'C': ['C8', 'C9', 'C10', 'C11'], 'D': ['D8', 'D9', 'D10', 'D11']}) frames=[df1,df2,df3] result=pd.concat(frames) result ``` ![](https://i.imgur.com/vmQDLOo.jpg) ```python= result=result.reset_index(drop=True) result ``` ![](https://i.imgur.com/uSxP403.jpg) ## CSS & HTML ![](https://i.imgur.com/tlUO0UO.jpg) ```python= #自動開啟GOOGLE MAP import requests import webbrowser address=input('請輸入地址:') webbrowser.open('http://www.google.com.tw/maps/search/'+address) ``` ![](https://i.imgur.com/Orsvuru.jpg) 他會直接打開google map然後直接幫你搜尋火車站 ```python= import requests import webbrowser param={'wd':'yahoo購物'} r=requests.get('https://www.baidu.com/s',params=param) print(r.url) print(r.text) webbrowser.open(r.url) ``` https://www.baidu.com/s?wd=yahoo%E8%B3%BC%E7%89%A9 #這是搜尋後的網址 <html> <head> <script> location.replace(location.href.replace("https://","http://")); </script> </head> <body> <noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript> </body> </html> #這個是HTML的內碼 google的參數是q key的部分要改q Beautiful Soup解析文件的方式 html.parser 相容性不加,普遍都用這種 lxml 速度較快 html5lib 解析力強