python 字典建立的几种方法

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4749

2018/09/09 18:56


>>> bob = { 'name':'Bob Smith','age':42,'pay':30000,'job':'dev'}
>>> sue = { 'name':'Sue Jones','age':45,'pay':40000,'job':'hdw'}
>>> bob['name'],sue['pay']
('Bob Smith', 40000)
>>> bob['name'].split()[-1]
'Smith'
>>> sue['pay']*=1.10
>>> sue['pay']
44000.0
>>> bob = dict(name='Bob Smith',age=42,pay=30000,job='dev')
>>> bob
{'pay': 30000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith'}
>>> sue
{'job': 'hdw', 'pay': 44000.0, 'age': 45, 'name': 'Sue Jones'}
>>>
>>> sue={}
>>> sue['name']='Sue Jones'
>>> sue['age']=18
>>> sue
{'age': 18, 'name': 'Sue Jones'}
>>> names=['name','age','pay','job']
>>> values=['Sue Jones',18,39999,'hdw']
>>> zip(names,values)
[('name', 'Sue Jones'), ('age', 18), ('pay', 39999), ('job', 'hdw')]
>>> sue=dict(zip(names,values))
>>> sue
{'job': 'hdw', 'pay': 39999, 'age': 18, 'name': 'Sue Jones'}
>>> fields=('name','age','pay','job')
>>> record=dict.fromkeys(fields,'?')
>>> record
{'job': '?', 'pay': '?', 'age': '?', 'name': '?'}

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1