05chapter字典和结构化数据

字典使用举例:

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
False

color为什么可以直接用?因为直接放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

你可能感兴趣的:(05chapter字典和结构化数据)