字典-----Python

字典结构: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')

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静海彭于晏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值