SPy*_*SPy 1 python dictionary python-3.x pandas
我有以下列表:
dimensionList = [{'key': 2109290, 'id': 'R', 'name': 'Reporter', 'isGeo': True, 'geoType': 'region'},
{'key': 2109300, 'id': 'C', 'name': 'Commodity', 'isGeo': False, 'geoType': None},
{'key': 2109310, 'id': 'P', 'name': 'Partner', 'isGeo': True, 'geoType': 'region'},
{'key': 2109320, 'id': 'TF', 'name': 'Trade Flow', 'isGeo': False, 'geoType': None},
{'key': 2109330, 'id': 'I', 'name': 'Measure', 'isGeo': False, 'geoType': None}]
Run Code Online (Sandbox Code Playgroud)
我想从这个列表中创建字典
需要'id'的值作为字典的Id和'name'作为字典的值
预期成绩:-
ResultsDict = {'R':'Reporter', 'C':'Commodity', 'P':'Partner', 'TF':'Trade Flow', 'I':'Measure'}
Run Code Online (Sandbox Code Playgroud)
用途dict comprehension:
d = {x['id']:x['name'] for x in dimensionList}
print (d)
{'R': 'Reporter', 'C': 'Commodity', 'P': 'Partner', 'TF': 'Trade Flow', 'I': 'Measure'}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |