字典使用舉例:
myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}
myCat['size']
'fat'
'My cat has ' + myCat['color'] + ' fur.'
'My cat has gray fur.'
字典和列表的區別
1.次序,列表的次序會決定兩個列表是不是相同,就算內容一樣,次序不同的兩個列表就是不同,但在字典中沒有order的概念,所以是相同的。例如:
spam = ['cats', 'dogs', 'moose']
bacon = ['dogs', 'moose', 'cats']
spam == bacon
False
eggs = {'name': 'Zophie', 'species': 'cat', 'age': '8'}
ham = {'species': 'cat', 'age': '8', 'name': 'Zophie'}
eggs == ham
True
2.由于字典沒有次序,所以他不能被分割成slice,比如列表可以spam[2:5]
字典的三種return類函數:
keys(),values(),items()
特點:
有點類似于list的展示方式,但不是真的list:a.他們不能像列表一樣被修改;b.不能使用append()等。
驗證key or value是否存在在字典中
用in 和not in可以驗證(和列表的驗證方法一樣),例如
spam = {'name': 'Zophie', 'age': 7}
'name' in spam.keys()
True
'Zophie' in spam.values()
True
'color' in spam
Falsecolor為什么可以直接用?因為直接放color的時候就代表color是key的意思。因為如果你想驗證一個值是不是一個key,可以直接把這個值放在公式中驗證。
get()類函數-查看是否有該值
如果在字典中沒有那個key和對應的值,而在編程中又要直接使用那個值,可以用get()直接搜索并填充該函數,例如
picnicItems = {'apples': 5, 'cups': 2}
'I am bringing ' + str(picnicItems.get('eggs', 0)) + ' eggs.'
'I am bringing 0 eggs.'
setdefault()類函數-增加字典中的items
如果在字典中沒有你要查的那個key和value時,使用setdefault()可以check該key并且在沒有該key的時候將該key和對應value植入到字典;如果已存在該key,則原本字典的值不變。
spam = {'name': 'Pooka', 'age': 5}
spam.setdefault('color', 'black')
'black'
spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}
spam.setdefault('color', 'white')
'black'
spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}
下面的題目里count[character]不太懂
message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
count = {}
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1
print(count)
{' ': 13, ',': 1, '.': 1, 'A': 1, 'I': 1, 'a': 4, 'c': 3, 'b': 1, 'e': 5, 'd': 3, 'g': 2, 'i':
6, 'h': 3, 'k': 2, 'l': 3, 'o': 2, 'n': 4, 'p': 1, 's': 3, 'r': 5, 't': 6, 'w': 2, 'y': 1}
刪除字典中的items
del 字典名[key名]
使用pprint.pprint()讓數據結構更美觀,但首先要import pprint
使用數據結構模擬現實世界的事物
一字棋
疑問1,https://automatetheboringstuff.com/chapter5/
疑問2,
List to Dictionary Function for Fantasy Game Inventory
Imagine that a vanquished dragon’s loot is represented as a list of strings like this:
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
Write a function named addToInventory(inventory, addedItems), where the inventory parameter is a dictionary representing the player’s inventory (like in the previous project) and the addedItems parameter is a list like dragonLoot. The addToInventory() function should return a dictionary that represents the updated inventory. Note that the addedItems list can contain multiples of the same item. Your code could look something like this:
def addToInventory(inventory, addedItems):
# your code goes here
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
The previous program (with your displayInventory() function from the previous project) would output the following:
Inventory:
45 gold coin
1 rope
1 ruby
1 dagger
Total number of items: 48