字典结构:XXX={'A':'bbb' , 'B':'ccc' }
访问字典中的值 :字典名[ 'A']
# Finding out the meaning of the word marathon
# dictionary[key]
webstersDict = {'person': 'a human being, whether an adult or child', 'marathon': 'a running race that is about 26 miles', 'resist': ' to remain strong against the force or effect of (something)', 'run': 'to move with haste; act quickly'}
webstersDict['marathon']
#输出:
'a running race that is about 26 miles'
更新字典 :
添加:
solution 1:字典名['C']='qwwe'
solution 2:字典名.update({ ' '})
删除:
del 字典名['C']
# add one new key value pair to dictionary
webstersDict['shoe'] = 'an external covering for the human foot'
# return the value for the 'shoe' key
webstersDict
#输出:
{'person': 'a human being, whether an adult or child',
'marathon': 'a running race that is about 26 miles',
'resist': ' to remain strong against the force or effect of (something)',
'run': 'to move with haste; act quickly',
'shoe': 'an external covering for the human foot'}
# update method, update or add more than key value pair at a time
webstersDict.update({'shirt': 'a long- or short-sleeved garment for the upper part of the body'
, 'shoe': 'an external covering for the human foot, usually of leather and consisting of a more or less stiff or heavy sole and a lighter upper part ending a short distance above, at, or below the ankle.'})
webstersDict\
#输出:
{'person': 'a human being, whether an adult or child',
'marathon': 'a running race that is about 26 miles',
'resist': ' to remain strong against the force or effect of (something)',
'run': 'to move with haste; act quickly',
'shoe': 'an external covering for the human foot, usually of leather and consisting of a more or less stiff or heavy sole and a lighter upper part ending a short distance above, at, or below the ankle.',
'shirt': 'a long- or short-sleeved garment for the upper part of the body'}
del webstersDict['resist']
print(webstersDict,'\n')
#输出:
{'person': 'a human being, whether an adult or child', 'marathon': 'a running race that is about 26 miles', 'run': 'to move with haste; act quickly', 'shoe': 'an external covering for the human foot, usually of leather and consisting of a more or less stiff or heavy sole and a lighter upper part ending a short distance above, at, or below the ankle.', 'shirt': 'a long- or short-sleeved garment for the upper part of the body'}
使用get()方法返回给定键的值
storyCount = {'is': 100, 'the': 90, 'Michael': 12, 'runs': 5}
print(storyCount.get('is'))
print(storyCount.get('the',9))
print(storyCount.get('th',90))
#输出:
100
90
90
删除键,但同时可以返回值 :
.pop()
count = storyCount.pop('the')
print(count)
#输出:
90
遍历字典:
solution 1:返回key直接print(字典名.key()) 返回value直接print(字典名.value())
solution 2:for key in 字典名 如何输出key
solution 3: for key, value in 字典名.items(): 然后两个都输出
# return keys in dictionary
print(storyCount.keys())
# return values in dictionary
print(storyCount.values())
#输出:
['is', 'runs', 'Michael']
[100, 5, 12]
# iterate through keys
for key in storyCount:
print(key)
#输出:
is
runs
Michael
# iterate through keys and values
for key, value in webstersDict.items():
print(key, value)
#输出:
('person', 'a human being, whether an adult or child')
('run', 'to move with haste; act quickly')
('shoe', 'an external covering for the human foot, usually of leather and consisting of a more or less stiff or heavy sole and a lighter upper part ending a short distance above, at, or below the ankle.')
('marathon', 'a running race that is about 26 miles')
('shirt', 'a long- or short-sleeved garment for the upper part of the body')