Classical Dictionary use function
Dict: A dictionary is created from a sequence of other mappings (such as other dictionaries) or (keys, values). Of course dict become a function is not very exact, it is essentially a type. Like List.
Copy Code code as follows:
items=[(' name ', ' Zhang '), (' Age ', 42)]
D=dict (items)
d[' name ']
Len (d): Returns the number of items
D[K]: Returns the value above the key K.
D[K]=V: Sets the value of K to K.
Del D[k]: Deletes this entry in the dictionary.
K in D: Check if D contains an entry with a key of K. Note: You can only find keys and cannot find values.
Simple Phone This example:
Copy Code code as follows:
# A Simple Database
# A dictionary with the person names as keys. Each is represented as
# Another dictionary with the Keys ' phones ' and ' addr ' referring to their phone
# number and address, respectively.
People = {
' Alice ': {
' Phone ': ' 2341 ',
' Addr ': ' Foo drive 23 '
},
' Beth ': {
' Phone ': ' 9102 ',
' Addr ': ' Bar Street 42 '
},
' Cecil ': {
' Phone ': ' 3158 ',
' Addr ': ' Baz Avenue 90 '
}
}
# Descriptive labels for the phone number and address. These would be used
# when printing the output.
Labels = {
' Phone ': ' Phone number ',
' Addr ': ' Address '
}
Name = Raw_input (' Name: ')
# Are We looking for a phone number or a address?
Request = Raw_input (' Phone number (p) or address (a)? ')
# Use the correct key:
If request = = ' P ': key = ' phone '
If request = = ' A ': key = ' addr '
# only try to print information if the name is a valid key in
# Our Dictionary:
If name in People:print '%s '%s ' s%s. '% \
(name, Labels[key], People[name][key])
Dictionary method
Clear: Clears all items in the dictionary.
Copy Code code as follows:
Copy: Shallow copy dictionary.
Copy Code code as follows:
Deepcopy: The same is copied, to see the difference with copy.
Copy Code code as follows:
From copy import deepcopy
d={}
d[' names ']=[' as ', ' sa ']
C=d.copy ()
Dc=deepcopy (d)
d[' names '].append (' ad ')
Fromkeys: Creates a new dictionary for the specified key, with the default value of None for each key.
Copy Code code as follows:
{}.fromkeys ([' Name ', ' age '])
Get: A more relaxed way to access dictionary entries.
Copy Code code as follows:
Copy Code code as follows:
# A Simple database using Get ()
# Insert Database (people) from Listing 4-1.
Labels = {
' Phone ': ' Phone number ',
' Addr ': ' Address '
}
Name = Raw_input (' Name: ')
# Are We looking for a phone number or a address?
Request = Raw_input (' Phone number (p) or address (a)? ')
# Use the correct key:
key = Request # In case the request is neither ' P ' nor ' a '
If request = = ' P ': key = ' phone '
If request = = ' A ': key = ' addr '
# Use Get to provide default values:
person = People.get (name, {})
Label = Labels.get (Key, key)
result = Person.get (key, ' not available ')
print '%s ' s%s is%s. '% (name, label, result)
Has_key: Check to see if the dictionary contains a given key. D.haos_key (). Value returns True, False.
Items: Returns a list of all dictionary items.
Iteritems: The method is roughly the same, but returns an iterator instead of a list.
Keys: Returns the keys in the dictionary as a list. (Note the difference between and items)
Iterkeys: Returns an iterator for the key.
Pop: Gets the value of the given key, and then deletes the key-value pair.
Popitem: pops up a random item,
SetDefault: You can get a value that is related to a given key, and you can set the corresponding key value without the key in the dictionary.
Update: Updating another dictionary with one dictionary.
Copy Code code as follows:
d={' 1 ': ' d ', ' 2 ': ' s ', ' 3 ': ' A '}
x={' 1 ', ' JK '}
D.update (x)
Values: Returns the value in the dictionary as a list.
Itervalues: Returns a worthy iterator.