On the sixth day, Dictionary Dictionary

  Dictionary (Dictionary) is a variable in Python container model which is obtained by a set of keys (key) value (value) of the composition, this type of construction is also commonly called mapping, or called associative array, also called hash table. Between each with key-value ":" separated, each with "," division, the entire dictionary as "{}" enclosed, and is defined as shown in FIG.  

  When the dictionary definition, the previous key, after the value, the key must be unique property values ​​may not be unique, if the same key, then take the last value;

  Value can be any data type, the data type of the key must be immutable (numbers, strings, component);

  Defined when the dictionary is {}, is a list of [], is a tuple ();

Python is integrated in many ways about the dictionary, let's briefly enumerate them:

clear () # empty dictionary data; a dictionary copy () # copies (shallow copy) in the transactions;

fromkeys () # establish dictionary using a given key, the default value corresponding to "None";

get (key, default = None) # access the dictionary corresponding to the value in the key, as the key to return to default value does not exist;

items () # Gets a dictionary of key data, returned in a list;

keys () # Get the data dictionary key, returns a list form; values () # acquires dictionary data value, returns a list form;

setdefault (key, default = None) # and get methods similar, except that, as the key does not exist, add it to the dictionary and the key value to the default value, if the key is present, and the key has a value , the value of the key is returned.

update (dict2) # dict2 the dictionary data (key-value pairs) updating the dictionary to another;

 

pop (key [, default]) value of a given key to delete the dictionary corresponding to the key, the return value is deleted. key value must be given. Otherwise, return default values.
popitem () returns a random and remove the pair of keys and values in the dictionary.

A = {>>> 'A':. 1, 'B': 2, 'C':}. 3
>>> A
{ 'A':. 1, 'B': 2, 'C':}. 3
>>> a [ 'a'] = 10 ####### modify the dictionary to a value of a button, 10
>>> a
{ 'a': 10, 'B': 2, 'C':. 3}

>>> b = a.items () ####### taking key, returns a tuple form
>>> B
dict_items ([( 'A',. 1), ( 'B', 2), ( 'c', 3)])

>>> a.keys () ### is not a list is not returned by the dictionary, it is a special type.
dict_keys ([ 'a', ' b', 'c'])

>>> b = list (a.keys () ) #### to the dictionary assigned to a key b in the form of a list.
B >>>
[ 'A', 'B', 'C']
>>>

>>> a.values()
dict_values([10, 2, 3])

print (dic5.pop ( 'age') ) # key to delete the specified dictionary and returns the key-value pairs 
RET = dic5.pop ( 'Age')
Print (RET)
Print (dic5)

A = dic5. popitem () # randomly delete a key-value pair and return values in a tuple
print (a, dic5)

= dict.fromkeys dic6 ([ 'host1', 'host2', 'host3'], 'Test') 
Print (dic6) #
### returns { 'host3': 'test' , 'host1': 'test', 'host2': 'test'}

traversal

# for i in dic5:
# print(i,dic5[i])
# for i,v in dic5.items():
# print(i,v)


A >>>
{ 'A':. 1, 'B': 2, 'C':}. 3
>>> B
{ 'A':. 1, 'B': 2}
>>> B [ 'B'] = 22 is
>>> B
{ 'a':. 1, 'B':} 22 is
>>> a.update (B) ##### update update the dictionary dict2 key / value pairs to update dict in
>>> A
{ 'A':. 1, 'B': 22 is, 'C':}. 3
>>>

String concatenation

>>> a='abc'
>>> b='123'
>>> c='@@@'.join([a,b])
>>> c
'abc@@@123'
>>>



 

Guess you like

Origin www.cnblogs.com/zpzhou/p/10985392.html