d[key]
:字典以鍵值key去存取資料value,很像是串列的索引indexint
、float
、str
、tuple
)d = {"Mars":20,"姿君":25,"毛毛":10}
d["Mars"] += 15
d["姿君"] += 5
print(d)
print(d["Mars"],d["姿君"])
{'毛毛': 10, '姿君': 30, 'Mars': 35} 35 30
d = {} # 空字典
d["Mars"] = 20
d["姿君"] = 25
d["毛毛"] = 10
print(d)
print(len(d))
{'毛毛': 10, '姿君': 25, 'Mars': 20} 3
print(d['MarsW'])
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-3-8ab309ac3ada> in <module>() ----> 1 print(d['MarsW']) KeyError: 'MarsW'
水果庫存、價格都以字典型別儲存,
請計算若水果都賣出後,可賺進多少錢?
stock={
"banana":5,
"apple":3,
"orange":10
}
price={
"banana":5,
"apple":20,
"orange":15
}
revenue = 0
revenue += stock["banana"] * price["banana"]
revenue += stock["apple"] * price["apple"]
revenue += stock["orange"] * price["orange"]
print(revenue)
235
revenue = 0
for fruit in stock:
print(fruit,stock[fruit],price[fruit])
revenue += stock[fruit] * price[fruit]
print(revenue)
apple 3 20 orange 10 15 banana 5 5 235
d = {} # 空字典
d["Mars"] = 20
d["姿君"] = 25
d["毛毛"] = 10
print(d)
d["Mars"] = 30
print(d)
{'毛毛': 10, '姿君': 25, 'Mars': 20} {'毛毛': 10, '姿君': 25, 'Mars': 30}
並不會保留本來「Mars:20」,然後再新增一個「Mars:30」
而是直接把Mars這個key指向30這個value
d = {} # 空字典
print(id(d))
d["Mars"] = 20
d["姿君"] = 25
d["毛毛"] = 10
print(id(d))
140553010539976 140553010539976
d.keys()
¶stock={
"banana":5,
"apple":3,
"orange":10
}
print(stock.keys())
for i in stock.keys():
print(i)
dict_keys(['apple', 'orange', 'banana']) apple orange banana
d.values()
¶stock={
"banana":5,
"apple":3,
"orange":10
}
print(stock.values())
for i in stock.values():
print(i)
dict_values([3, 10, 5]) 3 10 5
d.items()
¶stock={
"banana":5,
"apple":3,
"orange":10
}
print(stock.items())
for i in stock.items():
print(i)
dict_items([('apple', 3), ('orange', 10), ('banana', 5)]) ('apple', 3) ('orange', 10) ('banana', 5)
集合跟字典一樣也是
st = {1, 3.5, 'b', 2, 'a', 6, 'a', 2}
print(type(st))
print(st)
d = {}
print(type(d))
<class 'set'> {1, 2, 3.5, 6, 'b', 'a'} <class 'dict'>
set(s)
¶st = set()
print(type(st))
<class 'set'>
st = set("PyLadies")
print(st)
{'d', 's', 'y', 'a', 'e', 'P', 'L', 'i'}
st = set([1, 3.5, 'b', 2, 'a', 6])
print(st)
{1, 2, 3.5, 6, 'b', 'a'}
st = set(range(5))
print(st)
{0, 1, 2, 3, 4}
st = set({"banana":6,"apple":3})
print(st)
{'apple', 'banana'}
st = {1, 3.5, 'b', 2, 'a', [1,2]}
print(st)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-18-792f1266cfed> in <module>() ----> 1 st = {1, 3.5, 'b', 2, 'a', [1,2]} 2 print(st) TypeError: unhashable type: 'list'
st = set([1, 3.5, 'b', 2, 'a', [1,2]])
print(st)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-19-61479bafb898> in <module>() ----> 1 st = set([1, 3.5, 'b', 2, 'a', [1,2]]) 2 print(st) TypeError: unhashable type: 'list'
s = """
UK
Taiwan
USA
France
New Zealand
UK
France
"""
原字串.split(子字串)
原字串.strip()
set(可迭代者)
,串列就是可迭代者。s = """
UK
Taiwan
USA
France
New Zealand
UK
France
"""
print(s.split('\n'))
# 想想看strip()要放在split前面還是後面
['', 'UK', 'Taiwan', 'USA', 'France', 'New Zealand', 'UK', 'France ', '']
集合.add(不可變物件)
¶st = set()
print(id(st))
st.add(1)
st.add('a')
st.add(3.5)
st.add(2)
print(st)
print(id(st))
140553093892808 {1, 'a', 3.5, 2} 140553093892808
UK
Taiwan
USA
France
New Zealand
UK
France
st = set()
for i in range(__):
keyin = ______
st.___(keyin)
print(st)
st1 = {1, 2, 'a', 3.5}
st2 = st1.copy()
print("st1",st1)
print("st2",st2)
st1 {'a', 2, 3.5, 1} st2 {'a', 2, 3.5, 1}
集合.clear()
¶st = {1, 2, 'a', 3.5}
st.clear()
print(st)
set()
集合.pop()
¶st = {1, 2, 'a'}
print(st.pop())
print(st)
print(st.pop())
print(st)
print(st.pop())
print(st)
print(st.pop())
a {2, 1} 2 {1} 1 set()
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-24-fa3b9b14188d> in <module>() 6 print(st.pop()) 7 print(st) ----> 8 print(st.pop()) KeyError: 'pop from an empty set'
集合.remove(元素)
¶st = {1, 2, 'a'}
st.remove(2)
print(st)
st.remove('b')
{'a', 1}
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-25-3ac930768714> in <module>() 2 st.remove(2) 3 print(st) ----> 4 st.remove('b') KeyError: 'b'
集合.discard(元素)
¶st = {1, 2, 'a'}
st.discard(2)
print(st)
st.discard('b')
print(st)
{'a', 1} {'a', 1}
len(集合)
¶st = {1, 2, 'a'}
print(len(st))
3
7
UK
Taiwan
USA
France
New Zealand
UK
France
input()
,輸入結果為字串num = input()
st = set()
for i in range(____):
keyin = input()
st.add(keyin)
print(____)
元素 in 集合
元素 not in 集合
st = {1, 2, 'a'}
print('b' in st)
print('b' not in st)
False True
子集合<=父集合
子集合.issubset(父集合)
True
st0 = {1,2,'a'}
st1 = {'a'}
st2 = {3,'a'}
st3 = {1,2,'a'}
print('st1<=st0 ',st1<=st0)
print('st2.issubset(st0)',st2.issubset(st0))
print('st3.issubset(st0)',st3.issubset(st0))
st1<=st0 True st2.issubset(st0) False st3.issubset(st0) True
子集合<父集合
子集合.issubset(父集合)
and 子集合!=父集合False
st0 = {1,2,'a'}
st1 = {'a'}
st2 = {3,'a'}
st3 = {1,2,'a'}
print('st1<st0 ',st1<st0)
print('st2<(st0) ',st2<(st0))
print('st3<(st0) ',st3<(st0))
print('st3<=(st0)',st3<=(st0))
st1<st0 True st2<(st0) False st3<(st0) False st3<=(st0) True
父集合>=子集合
父集合.issuperset(子集合)
True
st0 = {2,'a'}
st1 = {1,3,'a'}
st2 = {1,2,'a'}
st3 = {2,'a'}
print('st1>=st0 ',st1>=st0)
print('st1.issuperset(st0)',st1.issuperset(st0))
print('st2.issuperset(st0)',st2.issuperset(st0))
print('st3.issuperset(st0)',st3.issuperset(st0))
st1>=st0 False st1.issuperset(st0) False st2.issuperset(st0) True st3.issuperset(st0) True
父集合>子集合
父集合.issuperset(子集合)
and 子集合!=父集合False
st0 = {2,'a'}
st1 = {1,3,'a'}
st2 = {1,2,'a'}
st3 = {2,'a'}
print('st1>st0 ',st1>st0)
print('st2>st0 ',st2>st0)
print('st3>st0 ',st3>st0)
print('st3>=st0',st3>st0)
st1>st0 False st2>st0 True st3>st0 False st3>=st0 False
集合1.isdisjoint(集合2)
st0 = {1,2,3,4,5}
st1 = {1,3,7}
st2 = {6,7,8}
print(st0.isdisjoint(st1))
print(st0.isdisjoint(st2))
False True
st0 = {1,2,3,4,5}
st1 = {1,3,7}
st2 = {6,7,8}
print(st0 | st1)
print(st0.union(st1))
print(st0 | st1 | st2)
print(st0.union(st1,st2))
print('st0 =',st0)
{1, 2, 3, 4, 5, 7} {1, 2, 3, 4, 5, 7} {1, 2, 3, 4, 5, 6, 7, 8} {1, 2, 3, 4, 5, 6, 7, 8} st0 = {1, 2, 3, 4, 5}
st0 = {1,2,3,4,5}
st1 = [1,3,7]
print(st0.union(st1))
print(st0 | st1)
{1, 2, 3, 4, 5, 7}
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-35-67f2a8e5e38f> in <module>() 2 st1 = [1,3,7] 3 print(st0.union(st1)) ----> 4 print(st0 | st1) TypeError: unsupported operand type(s) for |: 'set' and 'list'
集合1 |= 集合2 | ...
集合1.update(集合2,...)
st0 = {1,2,3,4,5}
st1 = {1,3,7}
st2 = {6,7,8}
st3 = {5,9}
st0 |= st1 | st2
print(id(st0),st0)
st0.update(st3)
print(id(st0),st0)
140553010225448 {1, 2, 3, 4, 5, 6, 7, 8} 140553010225448 {1, 2, 3, 4, 5, 6, 7, 8, 9}
st0 = {1,2,3,4,5}
st1 = {1,3,7}
st2 = {6,7,8}
print(st0 & st1)
print(st0.intersection(st1))
print(st0 & st1 & st2)
print(st0.intersection(st1,st2))
print('st0 =',st0)
{1, 3} {1, 3} set() set() st0 = {1, 2, 3, 4, 5}
集合1 &= 集合2 & ...
集合1.intersection_update(集合2,...)
st0 = {1,2,3,4,5}
st1 = {1,3,7}
st2 = {3,6,9}
st0 &= st1 & st2
print(st0)
{3}
集合1 - 集合2 - ...
集合1.difference(集合2,...)
st0 = {1,2,3,4,5}
st1 = {1,3,7}
st2 = {2,4,6,8}
print(st0 - st1)
print(st0.difference(st1))
print(st0 - st1 - st2)
print(st0.difference(st1,st2))
print('st0 =',st0)
{2, 4, 5} {2, 4, 5} {5} {5} st0 = {1, 2, 3, 4, 5}
集合1 -= 集合2 | ...
集合1.difference_update(集合2,...)
st0 = {1,2,3,4,5}
st1 = {1,3,7}
st2 = {2,4,6,8}
st0 -= st1 | st2
print(st0)
{5}
st0 = {1,2,3,4,5}
st1 = {1,3,7}
print(st0 ^ st1)
print(st0.symmetric_difference(st1))
print('st0 =',st0)
{2, 4, 5, 7} {2, 4, 5, 7} st0 = {1, 2, 3, 4, 5}
集合1 ^= 集合2
集合1.symmetric_difference_update(集合2)
st0 = {1,2,3,4,5}
st1 = {1,3,7}
st0 ^= st1
print('st0 =',st0)
st0 = {2, 4, 5, 7}
以下是PyLadies參加入門01、02活動的名單,可以告訴我們:
s1 = {'Cathy','Chloe Hsu','Claire Yu Shan OU','Croc East','Dana Tai','Dobe','Ella Cheng','EmilyChen TW','Eunice','F. Wu','Fion Chen','GloriaLee','Heidi Chieh-An Lin','Huilan','Jamie Yeh','Jessie Chou','Jovianne Yen Hsun Lin','Joy Chou','joyjhiang','Kate Lizzie','Kelly','Ling Huang','Mandy','MarsW','Mei-Yao Cheng','Michelle Y. Chou','Miko Liu Yiyu','Mini Hu','Nata','Pei Lee','pipi Lan','rose350051','Ruby Lu','sandy c','Sara','Sharon Lin','Sonia','Stella.Chang','Szu-ying Lee','Tao','Tina Han','Tracy','Vicky Tsai','Weishan','Xiang Ying Yao','YICHUN','YuLu Tung','Yuting Hung','Yvonne Huang','乃勤','卓家璘','姿君 (Kelly)','張耀文','林心誼','王翊帆','葉靜縈','譚余家','陳冠穎','陳品君','陳娜娜','陳宣瑀','黃媺芝'}
s2 = {'Angela Chang','Anna Yen','Annz Xue','Chloe Hsu','Croc East','Dana Tai','Dobe','Ella Cheng','Eugenia','F. Wu','Fion Chen','gabrielzzy','Heidi Chieh-An Lin','Irene Yu-hsuan Chen','Jovianne Yen Hsun Lin','Kelly','Kelly Hung','Mandy','MarsW','Michelle Y. Chou','Mini Hu','Pei Lee','Plurence','Po-Ying Fu','rose350051','Ruby Lu','Sonia','Tina Han','Tracy','Vicky','Weishan','Xiang Ying Yao','YICHUN','YuLu Tung','Yuting Hung','Yvonne Huang','乃勤','卓家璘','姿君 (Kelly)','張家瑜','林心誼','王翊帆','葉靜縈','陳冠穎','陳品君'}
len()
因為集合set
是可變型別,不能做字典的鍵值,也不能做集合的元素,
但Python另外還提供一個叫做frozenset
的集合型別,是不可變而且可雜湊。
st1 = {6,7,8}
st1.add(frozenset([1,2,3]))
print(st1)
st1.add(set([1,2,3]))
print(st1)
{8, frozenset({1, 2, 3}), 6, 7}
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-43-5769aed7f2e1> in <module>() 2 st1.add(frozenset([1,2,3])) 3 print(st1) ----> 4 st1.add(set([1,2,3])) 5 print(st1) TypeError: unhashable type: 'set'
set
相同fs = frozenset([1,2,3])
st = {1,3,5}
print(fs&st)
print(id(fs),fs)
fs &= st
print(id(fs),fs)
frozenset({1, 3}) 140553010242504 frozenset({1, 2, 3}) 140553093892584 frozenset({1, 3})
fs = frozenset([1,2,3])
fs.add(6)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-45-8ff2ceb2b608> in <module>() 1 fs = frozenset([1,2,3]) ----> 2 fs.add(6) AttributeError: 'frozenset' object has no attribute 'add'
st = {32,56,58,62,79,82,98}
st1=set()
for i in st:
if i<60:
st1.add(i)
print(st1)
{32, 56, 58}
st = {32,56,58,62,79,82,98}
st1 = {x for x in st if x<60}
print(st1)
{32, 56, 58}
d = {'a':32, 'b':56, 'c':58, 'd':62, 'e':79, 'f':82, 'g':98}
d1= {}
for key in d:
if d[key]<60:
d1[key]=d[key]
print(d1)
{'a': 32, 'c': 58, 'b': 56}
d = {'a':32, 'b':56, 'c':58, 'd':62, 'e':79, 'f':82, 'g':98}
d1= {key:value for key,value in d.items() if value<60}
print(d1)
# key,value翻轉
d2= {value:key for key,value in d.items() if value<60}
print(d2)
{'a': 32, 'c': 58, 'b': 56} {32: 'a', 56: 'b', 58: 'c'}
l = [32,56,58,62,79,82,98]
d = {}
for index,value in enumerate(l,ord("a")):
print(index,chr(index),value)
d[chr(index)]=value
print(d)
97 a 32 98 b 56 99 c 58 100 d 62 101 e 79 102 f 82 103 g 98 {'a': 32, 'd': 62, 'e': 79, 'f': 82, 'g': 98, 'c': 58, 'b': 56}
l = [32,56,58,62,79,82,98]
d = {chr(key):value for key,value in enumerate(l,ord("a"))}
print(d)
{'a': 32, 'd': 62, 'e': 79, 'f': 82, 'g': 98, 'c': 58, 'b': 56}
字典的鍵、集合的元素前面都說是不可變物件,
但正確說法是「可雜湊者」。
Python內建的不可變物件是可雜湊者,
可以透過雜湊演算法-內建函式hash()
,得到獨一無二的雜湊值,
不可變物件在存活期間,內容絕對不變,雜湊值也是獨一無二,也不會改變。
而相對的可變物件則沒有此特性,
因此嚴格說起來不可變!=可雜湊(可以自行定義型別)
hash()
會因操作環境不同而得到不同的結果d = {'apple':15, 'banana':30.5, 'orange':"$48"}
print(hash('apple'))
print(hash('banana'))
print(hash('orange'))
print(hash('apple'))
-6616432743583863425
-5363598976648845331
8163426422985150588
-6616432743583863425
s = {15, 30.5, "$48"}
print(hash(15))
print(hash(30.5))
print(hash("$48"))
15
1152921504606847006
8422802888266984843
所以其實字典、集合的示意圖是像以下這樣: