翻譯
int |
float |
str |
bool |
---|---|---|---|
整數 | 浮點數 | 字串 | 布林值 |
list |
dict |
tuple |
set |
---|---|---|---|
串列 | 字典 | 元組 | 集合 |
List 在 Python 中常常用來儲存連續性的資料,在使用上與 C/C++ 中的 陣列(array) 有許多相似之處。
不過list這個字本身就是不包含長度的意思,在建立之初不用(也不可以)宣告長度,用實際物品比喻的話可以理解為單字卡(下圖)
btw 如果想出建立有初始長度的請搭配generator使用
並且能藉由自帶的功能(methon)來實現元素的新增或移除
Python 建立 List 時以中括號將元素包起來,
值得一提的是:List 能將 不同型態 的資料放在一起
example = [123, 'word', True, [], {}]
索引 | example |
---|---|
0 | 123 |
1 | 'word' |
2 | True |
3 | [] |
4 | {} |
Dictionary 與 List 的區別就只是包著的符號改成大括號、需要特別宣告 鍵值(key) 而已。
example = {4:123, 2:'word', 0:True}
索引 | example |
---|---|
4 | 123 |
2 | 'word' |
0 | True |
dict
補充說明dict
是 無序 的。
dict
的 鍵值(key) 只可以是int
, str
, 或 tuple
ex1 = {3:[], 27:[]} # key 為 int 的 dict ex2 = {'m':[], 'l':[]} # key 為 str 的 dict ex3 = {(7,9):[], (4,2):[]} # key 為 tuple 的 dict
tuple, set
補充tuple, set
類似 list
什麼都能塞,但
t = (1,'words',False) # tuple 不能變動 s = {0.0,True,'luv'} # set 無法取 index
tuple, set, list
可以互相 轉型
t = (1,'words',False) s = {2.0,True,'luv'} l = [False,0,'abc'] #下面隨便看看就好,傷眼睛 ts = tuple(s) #ts = (2.0,True,'luv') tl = tuple(l) #tl = (False,0,'abc') st = set(t) #st = {1,'words',False} sl = set(l) #sl = {False,0,'abc'} lt = list(t) #lt = [1,'words',False] ls = list(s) #ls = [2.0,True,'luv']
ex = {'a':(True, False), 'b':[3, 2, 7]} print(ex['b'][2]) #輸出: 7
建立一個包含
全是 int
的 list
和全是 bool
的 tuple
、
索引值為 str
的 dict
索引 | ex |
---|---|
a | (True, False) |
b | [3, 2, 7] |
list, dict
中要存取其中的某一筆資料,直接在後面加上 [index]
即可
e = ['word', 123, False] print(e[0]) # list 索引值從 0 開始 #輸出: word d = {'m':420,'w':327,'d':105} print(d['w']) # 字串索引 dict 取元素需打上'' #輸出: 327
補充: str
取索引值會取得位於該索引值上的字元
w = 'lth' print(w[2]) #輸出: h
list
中如果取 index 為 -1 即為 list
中最後一個元素
e = ['word', 123, False] print(e[-1]) #輸出: False print(e[-2]) #輸出: 123 ...依此類推
注意: index 取負號只限 list
或 tuple
e = ['word', 123, False] e[-1] = 2.0 print(e) #輸出: ['word', 123, 2.0] d = {'m':420,'w':327,'d':104} d['d'] += 1 #在原有的數字上+1 print(d) #輸出: {'m':420,'w':327,'d':105}
將字串:'hello'
取代為 list:['hello']
ex[0] = ['hello']
試試看 在以下程式碼中間加一行使輸出為 hello
ex = ['hello', 123, False] #加在這 print(ex[0][0]) #原輸出: h
list
取範圍List 中還有一個特性就是能指定範圍。
使用中括號 [a:b]
即為 list
索引值 a~b 的部分,如
list_example=['0',1,'2',3,'4',5,'6'] print(list_example[2:-1]) #[:-#]有別於[-#:]、[-#] #輸出:['2', 3, '4', 5]
取範圍還有其他特定位置的用法,如
省略起始: [:9]
\(\iff\)[0:9]
省略結尾: [1:]
\(\iff\)[1:len(list)]
取範圍同樣適用 str, tuple
,但 tuple
只能取、不能動。另因為 dict
無序,無法取範圍。
len()
list
有幾個元素、或dict
有幾組資料print(ex[-4:]) #輸出: hijk print(ex[:5]) #輸出: abcde print(ex[1:-4]) #輸出: bcdefg
試對字串 ex
取範圍
ex = 'abcdefghijk'
輸出
hijk
abcde
bcdefg
list
新增元素append(object)
append()
能將資料新增在list 的結尾
格式: append(
你要插入的資料)
e = list() #同 e = [] e.append(3.27) e.append('x') print(e) #輸出[3.27, 'x']
insert(int,object)
insert()
能將元素插入指定位置
格式: insert(
你要插入的index,
你要插入的元素)
e = [6, 'x', 9, 'y'] e.insert(2,True) print(e) #輸出: [6, 'x', True, 9, 'y']
dict
新增元素dict
中新增元素需要搭配鍵值(key/index)新增,方法有兩種
d = dict() #同 d = {} #方法1: 取想新增的索引值後賦值,常用 d['d'] = 105 #方法2: 運用setdefault()成對塞進去,限鍵值不存在時使用 d.setdefault('x', True) print(d) #輸出: {'d': 105, 'x': True}
setdefault()
延伸d = dict() # setdefault() 常用來新增 list 或 set 後進行操作 d.setdefault('e',[4.2]).append(3.27) d.setdefault('s',{7.1}).add(105)# set 新增元素用 add() print(d) #輸出: {'e': [4.2, 3.27], 's': {105, 7.1}} test = d.setdefault('e') #有了鍵值之後後面打啥都沒用 print(test) # 看看這是什麼 #輸出: [4.2, 3.27] print(d['e']) # 再看看這是什麼 #輸出: [4.2, 3.27]
d['a'].insert(1,'C') d['a'].append('U') d['b'] = 'IT' #或 d.setdefault('b','IT')
不使用 元素變換,試將 d
透過 新增元素 變成 dd
。
d = {'a':['N','H']} #新增元素 dd = {'a':['N','C','H','U'], 'b':'IT'} print(d,'' if(d == dd) else 'Wrong answer')
remove(object)
使用 remove()
可以移除 list
中的指定資料
格式: remove(
你要移除的資料)
e = ['0',1,'2',3] e.remove('0') e.remove(1) print(e) #輸出: ['2', 3]
remove()
適用 list, set
,dict
無法移除指定資料pop(int/str)
pop()
可以根據輸入的 index 位置來移除元素
格式: pop(
想移除元素的索引值/鍵值)
e = ['a',1,'2',3] e.pop(2) print(e) #輸出: ['a', 1, 3] e.pop() #list 中如果空白的話則會移除最後一個 print(e) #輸出: ['a', 1]
另外 pop()
是能回傳值的,如
e = ['x',1,'2',3] return_pop = e.pop(0) print(return_pop) #輸出: x
d['a'].pop() #或 pop(3) 或 remove('U') d['a'].pop(1) #或 remove('C') d.pop('b')
不使用 元素變換,試將 d
透過 移除元素 變成 dd
。
d = {'a':['N','C','H','U'], 'b':'IT'} #移除元素 dd = {'a':['N','H']} print(d,'' if(d == dd) else 'Wrong answer')
有趣的題外話:
JSON
d={
'PlayName':'Mandy',
'BagItem':{
'sword':{
'LV':5,
'atk':2
},
'hat':{
'LV':10,
'def':3
}
},
'Equipment':{
'GM hat':{
'LV':1,
'def':999
},
'GM Gun':{
'LV':1,
'atk':999
}
}
}
程式寫作時,我們常常需要重複執行某段程式,這時迴圈的使用就顯得很重要
當我們已知or可計算執行的次數時我們常用for迴圈
而當執行次數難以計算時,就比較適合用while迴圈
用法:
for (一物件) in (所有物件) :
簡單來說 就是
for "自己取的變數名稱" in "list,taple....":
"你要執行的程式碼"
PS:注意縮排
我們每次執行 都會從 後面的 list 依序 拿其中一個元素對他做處理
Q:那我只是單純的想做100次呢?
那就用"range()"
for 後面接一個 range() 如
for i in range(5): print(i) # 0,1,2,3,4
class range(stop)
class range(start, stop[, step])
用法
# 0,1,2,3,4,....29
# 5,6,7,8,9,10...29 #是5到29
# 5,10,15,20,25
當然數字不一定是要正的,負的也行
k = int(input("input: ")) for i in range(k, -1, -1): print(i, end=" ")
輸入:n
輸出:n 到 0
ex:10,9,8,7,6,…0
hint:
注意range()的範圍
x = int(input()) #輸入一個數字,並轉為整數的型態 print(x) #輸出一個數字
n = int(input()) fb_list=[] for i in range(n): if i == 0 or i == 1: fb_list.append(1) else : fb_list.append(fb_list[-1]+fb_list[-2]) print(fb_list)
輸入:n
輸出:F1~Fn 的費氏數列
ex:
input:6
output:1,1,2,3,5,8,
input:8
output:1,1,2,3,5,8,13,21,
hint:
用list存
for 後面接一個list
舉例來說
list=[2,4,5,8,1,3] for x in list: print(x) # 2,4,5,8,1,3
當然,不一定要都是相同的型態
list=['gawr','gura',1,0,0,'!'] for x in list: print(x,end='')
list_A=[1,2,3,54,4,4,6,8] for i in list_A: print(2*i, end=' ')
給定
list_A=[1,2,3,54,4,4,6,8]
輸出:
兩倍的list_A
ex:
[2,4,6,108,8…]
list_B = [1,2,3,54,4,4,6,8] list_C = [] for i in range(-1, -len(list_B)-1, -1): list_C.append(list_B[i]) for i in list_C: print(i, end=" ")
給定
list_B=[1,2,3,54,4,4,6,8]
輸出:
新的list_C=list_B的反過來
ex:
[8,6,4,4,54,3,2,1]
hint: Using "insert()" to add item in new list
防風林外還有防風林外還有防林
for 迴圈中當然也還能有for 迴圈
而要怎麼判斷屬於哪層的for迴圈則是看縮排
list_G=[['x1','x2','x3'],['y1','y2','y3'],['z1','z2','z3']] for dim in list_G: for pos in dim: print(pos)
上面這 第一個 dim指的是 list 中的 list
a=10 b=20 count=0 for i in range(a): for j in range(b): count += 1 #count = count + 1 print(count)
猜猜看會輸出多少?
因為內層的 for 執行了20次count+=1
而外層的 for 執行了內層的 for 10次
所以20*10=200
a=10 b=20 count=0 for i in range(a): count+=1 for j in range(b): count+=1 print(count)
那這樣是多少呢?
for 搭配 if
a=int(input("多高")) listA=['gura',1,0,0,'!'] for x in listA: print(x,end=' ') print('') for i in range(a): if i==(int)(a/1.5): for j in range(a+i+1): if j>=a-i and j<=a+i+1: print('*',end='') else: print(' ',end='') else: for j in range(a): if j==a-i: print('*',end='') else: print(' ',end='') for j in range(i+1): if j==i: print('*',end='') else: print(' ',end='') print('')
a=int(input()) for i in range(a): for j in range(a): print('*',end='') print('')
輸入 n
印出 n*n的正方形
ex:
input=3
output=
***
***
***
a=int(input()) count=0 for i in range(a): for j in range(1, i//2): if (i%j)==0: count+=1 if count==1: print(i,end=',') count=0
輸入 n
印出 所有小於n的質數
ex:
input=15
output=2,3,5,7,11,13
輸入 n
印出 半徑為n的圓形
you may need sqrt() and round()
import math r=int(input()) count=0 for i in range(2*r+1): for j in range(2*r+1): if (i-r)**2+(j-r)**2<=r**2 and (i-r)**2+(j-r)**2>=r**2 : print('*',end='') else: print(' ',end='') print('')
whlie 通常用於我們不知道做幾次,但是知道甚麼時候該結束的事件
a=2 while(a<4096): a*=a print(a)
上面範例就是a一直平方直到a>=4096
a=1 k=' ' while(k): k=input() a+=1 print(a,k) print('--end--')
這段程式碼則是會在k沒有輸入時停止
在執行到一半時,如果我們希望能直接跳出迴圈則使用break
break!
a=1 k=' ' while(k): k=input() if k=='exit': break a+=1 print(a,k) print('--end--')
上面程式在k==exit時會直接跳出while loop
a=1 k=' ' while(k): k=input() if k== 'pass': continue a+=1 print(a,k) if k=='exit': break print('--end--')
input 整數n ,n>0 請輸出 n 的二進位數
a=int(input()) output='' while(a>0): output=str(int(a%2))+output a=a-(a%2) a=a/2 print(output)
ex:
input:4
output:100
input:15
output:1111
舉例來說
list=[2,4,5,8,1,3] for x in list: print(x) # 2,4,5,8,1,3
不過這邊的運算大概是這樣
list=[2,4,5,8,1,3] for i in range(len(list)): x=list[i] print(x)
.
.
.
.
a=[-1,2,-3,-4,5,6,7,-8,9,-10,11] for x in a: x+=10 print(a)
對x的操作只影響x
不影響list
.
.
.
a=[-1,2,-3,-4,5,6,7,-8,9,-10,11] for x in a: if x<0: a.remove(x) print(a)
a=[-1,2,-3,-4,5,6,7,-8,9,-10,11] for x in a[:]: if x<0: a.remove(x) print(a)
如果對list直接操做要注意是否會影響跌代的結構
for 迴圈其實是呼叫一個迭代器
所以可以透過自訂迭代邏輯來客製化迴圈
ex:
class MyIterator: def __init__(self): self.total_times = 0 self.index = 0 def add_times(self, times): self.total_times += times return self def __iter__(self): return self def __next__(self): self.index += 1 if self.index <= self.total_times: return self.index else: self.index -= 1 raise StopIteration my_iterator = MyIterator() for item in my_iterator.add_times(3): print(item) for item in my_iterator.add_times(3): print(item) for item in my_iterator: print(item)
https://leetcode.com/problems/two-sum/
https://leetcode.com/problems/add-two-numbers/
https://leetcode.com/problems/climbing-stairs/