什么是 Dictionary 呢?想了解 Dictionary 之前,可以先想像你現(xiàn)在手上有一本電子英英字典,當(dāng)你輸入英文單字的時候,就可以查得到它的唯一翻譯。也就是說,你所關(guān)心的 英文單字 與 翻譯 之間有著 一對一 的關(guān)系。你輸入的英文單字,就叫做 Key ;而得到的翻譯,就叫做 Value 。一個 Dictionary 是一群 Key : Value 配對的集合。
建立一個 Dictionary 物件其實很簡單,讓我們先看個例子:
d = {'python': 'Large constricting snakes.', 'ruby': 'A precious stone that is a red corundum.'}
當(dāng) Python 直譯器執(zhí)行這段程式后,Dictionary 物件就生成了。'python'
這個字串,在上例中即扮演 Key 的角色,而 'Large constricting snakes.'
這個字串,則扮演 Value 的角色。而 Key : Value 之間以冒號 : 間隔之,如果有好幾對 Key : Value,則以逗號 ,
間隔之。
當(dāng)我們使用 []
這個運算子的時候,我們就可以拿到它的 Value 了
print d['python']
則會印出
Large constricting snakes.
有的時候會想要知道 Dictionary 物件中,到底有幾對 Key, Value ,就如同取得字串的長度般,只要使用 len(d)
即可得到之。
print len(d)
則會印出
2
假設(shè)我們有新的單字出現(xiàn)了,想要新增到剛才的 Dictionary 物件中,該如何做呢? 同樣地,也是使用 []
這個運算子,這次還要配合上 =
這個運算子。
d['game'] = 'Activity engaged in for diversion or amusement.'
print d
則會印出
{'python': 'Large constricting snakes.', 'game': 'Activity engaged in for diver
ion or amusement.', 'ruby': 'A precious stone that is a red corundum.'}
我們可以發(fā)現(xiàn), d
這個物件中的確新增了一筆資料。
又假設(shè)我們想要修改某個單字的翻譯,也是用樣使用 []
這個運算子并要配合上 =
這個運算子:
d['python'] = 'A very powerful scripting language.'
print d
則會印出
{'python': 'A very powerful scripting language.', 'game': 'Activity engaged in for diver
ion or amusement.', 'ruby': 'A precious stone that is a red corundum.'}
我們可以發(fā)現(xiàn), 原來的 python
這個單字所對應(yīng)到的翻譯已經(jīng)被改變了。
假設(shè)我們現(xiàn)在要移除一個單字及其翻譯,我們可以利用 del
來達(dá)成這個目的。
del d['game']
print d
則會印出
{'python': 'A very powerful scripting language.', 'ruby': 'A precious stone that is a red corundum.'}
如此一來,'game'
這筆資料就被你刪除了。
print d['never_seen']
其中, 'never_seen'
并不在 Dictionary 之中,會發(fā)生什么事呢?print 'python' in d
print 'never_seen' in d
并觀察他們的不同之處。
我們已經(jīng)學(xué)會如何使用 Dictionary 來建立物件之間的一對一關(guān)系了,下一節(jié)將介紹如何使用 if
來做有條件地執(zhí)行某些程式。
更多建議: