5.1 dictionary data types
Dictionary Index can use many different types of data, not just integers. Index dictionary is called "key", the key and its associated value is referred to as "key - value" pairs in the code, when the input dictionary with curly braces {}.
Dictionary entries are not ordered, so the dictionary does not list as slice images.
5.1.1 Keys (), values () and items () method
key (), values () and items () method returns a list of similar value, respectively, corresponding to dictionary key, and the key value - value pairs. The real value is not a list returned by these methods, they can not be modified, no append () method. However, these data types may be used for loop.
>>> spam = {'color':'red','age':42}
>>> for i in spam.values():
print (i)
red
42
Can () method to convert the dictionary through the list as a list
>>> list(spam.keys())
['color', 'age']
>>> list(spam.values())
['red', 42]
>>> spam
{'color': 'red', 'age': 42}
5.1.2 GET () method setdefault () method
get () method takes two parameters: To obtain the value of the key, and if the key does not exist, an alternate return value
setDefault () method provides a way to pass a first parameter of the method is to check the key, the second parameter is a value to be set if the key does not exist. If the key exists on the return key.
If the program introduced the pprint () module, you can use the pprint () and pformat () print dictionary.
import pprint
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(pprint.pformat(count))
#pprint.pprint(count) print(pprint.pformat(count))这两种表达式等价
operation result:
{' ': 13,
',': 1,
'.': 1,
'A': 1,
'I': 1,
'a': 4,
'b': 1,
'c': 3,
'd': 3,
'e': 5,
'g': 2,
'h': 3,
'i': 6,
'k': 2,
'l': 3,
'n': 4,
'o': 2,
'p': 1,
'r': 5,
's': 3,
't': 6,
'w': 2,
'y': 1}
5.2 Practice Program
- Fun game of list of items
You create a fun video game. For data structure modeling on the players list item is a dictionary. Where the key is a string, describing the list of items, the value is an integer value, indicating how many players the article. For example, dictionary values { 'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12} means that the player has a rope, six torches, 42 gold and so on. Write a function named displayInventory (), which accepts any list of possible items and is shown below:
Inventory:
1 rop
6 torch
42 gold coin
1 dagger
12 arrow
Total number of items : 62
code show as below:
def displayInventory(dic):
print('Inventory:')
count = 0
for k, v in dic.items():
print(str(v) + ' ' + k)
count = v+count
print('Total number of items : ', count)
dicValue = {'rop': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
displayInventory(dicValue)
- The conquest of one-stop assuming that this trophy is expressed as a list of strings:
dragonLoot = ['gold coin', 'digger', 'gold coin', 'gold coin', 'ruby']
Write a function addToInventory (inventory, addedItems) name, where inventory is a dictionary representing the player's list of items (the same as the previous item), addedItems argument is a list, just like dragonLoot. addToInventory () function should return a dictionary representing the updated list of items.
def displayInventory(dic):
print('Inventory:')
count = 0
for k, v in dic.items():
print(str(v) + ' ' + k)
count = v+count
print('Total number of items : ', count)
def addToInventory(inventory, addeditems):
for i in addeditems:
if i in inventory.keys():
inventory[i] += 1
else:
inventory.setdefault(i, 1)
return inventory
inv = {'gold coin':42, 'rope':1}
dragonLoot = ['gold coin', 'digger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv,dragonLoot)
displayInventory(inv)
Previous program (plus the previous project displayInventory () function) the following output:
Inventory:
45 gold coin
1 rope
1 digger
1 ruby
Total number of items : 48