程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Learn Python from the turtle up master -- dictionary part 2

編輯:Python

Change : Sequence like operation , Just specify a key that exists in the dictionary , You can modify the corresponding value , If you want to modify multiple values , have access to update() Method .

>>> d = dict.fromkeys("FishC")
>>> d
{'F': None, 'i': None, 's': None, 'h': None, 'C': None}
>>> d['s'] = 115
>>> d
{'F': None, 'i': None, 's': 115, 'h': None, 'C': None}
>>> d.update({'i':105, 'h':104})
>>> d
{'F': None, 'i': 105, 's': 115, 'h': 104, 'C': None}
>>> d.update(F = '70', C = '67')
>>> d
{'F': '70', 'i': 105, 's': 115, 'h': 104, 'C': '67'}
>>> d.update(F = 70, C = 67)
>>> d
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67}

check : There are two ways ,1. The first is that you give a key , Return its corresponding value .2. The second is to use get() Method ,

>>> d['C']
67
>>> d.get('c', ' Not found ')
' Not found '
>>> d.get('C', ' Not found ')
67
>>> d.get('c')                     

Pay attention to the use of get Method time , By default, if it is not found, the second parameter can be set, such as in the code :' Not found ', If you find it , This key returns the corresponding value , By default, the second parameter does not display anything , Just skip to the next line .

setdefault(): Find out if a key exists in the dictionary , If it exists, its corresponding value is returned , If it doesn't exist, give it a new value .

>>> d.setdefault('C', 'code')
67
>>> d.setdefault('c', 'code')
'code'
>>> d
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67, 'c': 'code'}

items() 、keys()、values(): The three methods obtain the key value pairs of the dictionary respectively 、 key 、 value . The three are view objects , A view object is a dynamic view of a dictionary , It means that when the content of the dictionary changes , The view object changes accordingly .

>>> keys = d.keys()
>>> values = d.values()
>>> items = d.items()
>>> items
dict_items([('F', 70), ('i', 105), ('s', 115), ('h', 104), ('C', 67), ('c', 'code')])
>>> keys
dict_keys(['F', 'i', 's', 'h', 'C', 'c'])
>>> values
dict_values([70, 105, 115, 104, 67, 'code'])
>>> d.pop('c')
'code'
>>> d
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67}
>>> keys
dict_keys(['F', 'i', 's', 'h', 'C'])
>>> values
dict_values([70, 105, 115, 104, 67])
>>> items
dict_items([('F', 70), ('i', 105), ('s', 115), ('h', 104), ('C', 67)])

Shallow copy :copy() Method

>>> e = d.copy()
>>> e
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67}

Get the number of key value pairs :len() 

>>> len(d)
5

Determine whether a key exists in the dictionary :in perhaps not in

>>> 'C' in d
True
>>> 'c' in d
False
>>> 'c' not in d
True

Dictionaries can be converted into lists :list(), All in the dictionary key Composition list .

>>> list(d)
['F', 'i', 's', 'h', 'C']
>>> list(d.values)
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    list(d.values)
TypeError: 'builtin_function_or_method' object is not iterable   

The cause of the error should be the version problem , By default list() Just make a list of keys .

iter() function : It can also be used in dictionaries , The keys of the dictionary form an iterator .

>>> e = iter(d)
>>> next(e)
'F'
>>> next(e)
'i'
>>> next(e)
's'
>>> next(e)
'h'
>>> next(e)
'C'
>>> next(e)
Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    next(e)
StopIteration                                        // It's over. It's over

nesting

>>> d = {' Lyu3 bu4 ':{'chinese':66, 'math':99}, ' Guan yu ':{'chinese':88, 'math':77}}
>>> d[' Lyu3 bu4 ']['math']
99
>>> d = {' Lyu3 bu4 ':[66, 99], ' Guan yu ':[88, 77]}
>>> d[' Guan yu '][0]
88

Dictionary derivation


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved