Python - Demo11-- list type: a dictionary

dictionary

Explanation:

  • Dictionaries and lists are the same as a worthy collection;
  • Dictionary Index not like that list must be an integer, a dictionary index can be of many types;
  • Index dictionary is called "button", and the value of the key associated with the key together referred to as "key-value" pairs.

grammar:

{"key1":value,"key2":value,....}或者{'key1':value,'key2':value,....}

Example:

>>> mydic={'name':'bobo','age':12,'bobby':'apple'}
>>> mydic
{'name': 'bobo', 'age': 12, 'bobby': 'apple'}
>>> mydic['age']
12
>>> complexdic={'id':1,'content':mydic,'range':10}
>>> complexdic
{'id': 1, 'content': {'name': 'bobo', 'age': 12, 'bobby': 'apple'}, 'range': 10}
>>> complexdic['content']
{'name': 'bobo', 'age': 12, 'bobby': 'apple'}
>>> complexdic['content']['name']
'bobo'

Description: dictionaries and lists by index value, dict [ 'keys']; type value corresponding to the key dictionary may also be a dictionary.

Dictionary and list:

Dictionary key-value pair is not stored in the order, so long as the number of key-value pairs, as the content, then the two are equal dictionaries

Example:

>>> mydic
{'name': 'bobo', 'age': 12, 'bobby': 'apple'}
>>> mydd={'name':'bobo',"bobby":'apple',"age":12}
>>> mydd
{'name': 'bobo', 'bobby': 'apple', 'age': 12}
>>> mydd==mydic
True
>>> mylis=[1,2,3]
>>> mylis
[1, 2, 3]
>>> myll=[1,3,2]
>>> myll
[1, 3, 2]
>>>=='re wrong Mylla
False

Description: The dictionary is not stored in the order of the list is stored sequentially. Dictionary-slice images as a list.

Dictionary traversal methods:

keys (), values ​​(), items (): Get all available keys, respectively, to obtain an overview of all values, fetch all keys (this value is tuple)

Example:

>>> mydic
{'name': 'bobo', 'age': 12, 'bobby': 'apple'}
>>> mydic.keys()
dict_keys(['name', 'age', 'bobby'])
>>> for x in mydic.keys():
...     print(x)
...
name
age
bobby
>>> for x in mydic.values():
...     print(x)
...
bobo
12
apple
>>> for x,y in mydic.items():
...     print(x,y)
...
name bobo
age 12
bobby apple

Check dictionary key, the value of the presence of

Note: Before accessing the value of a key, the key to determine whether there is important in the dictionary.

get () method:

Two parameters, a first name is a bond; a second predetermined value; when the key corresponding to the key value does not exist or do not exist, we can return to the specified value.

Example:

>>> mydic
{'name': 'bobo', 'age': 12, 'bobby': 'apple'}
>>> key_value=mydic.get('adress',-1)
>>> key_value
-1
>>> key_value1=mydic.get('name','不存在')
>>> key_value1
'bobo'

Description: When there is a value corresponding to the key, get returns the value corresponding to the key in the dictionary; if there is no will return the value specified by us

setDefault () Method:

When faced with a key to set a default value, setdefault () method will be very convenient. It is built-checking mechanism to help us determine whether the key is a good solution to the problems.

Example 1: We do not have setdefault (practices when):

>>> mydic
{'name': 'bobo', 'age': 12, 'bobby': 'apple'}
>>> if 'color' in mydic:
...     pass
... else:
...     mydic['color']='yellow'
...
>>> mydic
{'name': 'bobo', 'age': 12, 'bobby': 'apple', 'color': 'yellow'}

Example 2: When the setDefault () Method:

>>> mydic
{'name': 'bobo', 'age': 12, 'bobby': 'apple', 'color': 'yellow'}
>>> mydic.setdefault('adress','china')
'china'
>>> mydic
{'name': 'bobo', 'age': 12, 'bobby': 'apple', 'color': 'yellow', 'adress': 'china'}
>>> mydic.setdefault('adress','usa')
'china'
>>> mydic
{'name': 'bobo', 'age': 12, 'bobby': 'apple', 'color': 'yellow', 'adress': 'china'}

Description: setdefault () method to check the value corresponding to the key not, adds the key to the dictionary, and we set the default value specified. When the discovery key has a value corresponding to the keys will not re-assign the corresponding value.

Example 3: give you a bunch of strings, requires you to be able to calculate each character appears several times in a string?

Ideas: Character - the number of times, which is obviously want us to use a dictionary expensive. As a key character - the number of times as the value.

Example:

= myStr ' alsjdnwlsdisla ' 
count_dic = {}
 for Item in myStr: 
    count_dic.setdefault (Item, 0 ) # key added to give the dictionary to prevent the key being given to the set value 
    count_dic [Item] = [Item] count_dic + . 1 # by assigning an index value to the index number corresponding to each iteration plus a 
print (count_dic)

Output:

{'a': 2, 'l': 3, 's': 3, 'j': 1, 'd': 2, 'n': 1, 'w': 1, 'i': 1}

 

Guess you like

Origin www.cnblogs.com/bigbosscyb/p/12325682.html