Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字典中获取密钥标签?_Python_List_Dictionary - Fatal编程技术网

Python 从字典中获取密钥标签?

Python 从字典中获取密钥标签?,python,list,dictionary,Python,List,Dictionary,我有以下字典,我想要每个城市的所有钥匙(例如,肾上腺素或空气等) dictionary = {u'adrenaline': {u'estimated_total': 234, u'more': True, u'results': [{u'adrenaline_score': 2.11474252688113, u'name': u'Rome'}, {u'adrenaline_score': 1.94859310493828, u'name': u'Budapest'},

我有以下字典,我想要每个城市的所有钥匙(例如,肾上腺素或空气等)

dictionary = {u'adrenaline': {u'estimated_total': 234,
u'more': True,
u'results': [{u'adrenaline_score': 2.11474252688113, u'name': u'Rome'},
             {u'adrenaline_score': 1.94859310493828, u'name': u'Budapest'},
             {u'adrenaline_score': 2.65914794847249, u'name': u'Ubud'},
             {u'adrenaline_score': 1.94859310493828, u'name': u'Calgary'},
             {u'adrenaline_score': 3.0, u'name': u'Canc\xfan'}]},
u'air': {u'estimated_total': 234,
u'more': True,
u'results': [{u'air_score': 1.50402325242855, u'name': u'Paris'},
           {u'air_score': 1.50402325242855, u'name': u'Rome'},
           {u'air_score': 1.50402325242855, u'name': u'New York City'},
           {u'air_score': 1.50402325242855, u'name': u'Abu Dhabi'},
           {u'air_score': 1.50402325242855, u'name': u'Key West'},
           {u'air_score': 1.50402325242855, u'name': u'Salt Lake City'}]}
与城市相关的标签(钥匙)和分数

输出:

     {'Rome': {'adrenaline': 2.11474252688113, 'air': 1.50402325242855},
      'Budapest': {'adrenaline': 1.94859310493828},
      'Ubud': {'adrenaline': 2.65914794847249},
      'Calgary': {'adrenaline': 1.94859310493828}....}

重命名dict
d
以不使用保留关键字
dict
,您可以执行类似操作(根据您想要的输出进行调整,但不清楚):


所以你想要一个新的字典,其中键是城市,值是空气分数或肾上腺素分数?预期输出是什么?像
{'Rome':'adrenaline','Budapest':'adrenaline','Paris':'air',…}
?另外,你真的不应该给字典命名,因为这会覆盖一个内置名称。你想要的输出是不清楚的,请编辑您的答案以包含所需的输出。我看到您编辑了问题,但您想要的输出仍然不清楚。。。不要说“与他们的分数”,请显示一个实际输出与分数太。。。
In [1]:
result = {}
for k, v in d.items():
    for score_row in v['results']:
        city = score_row['name']
        if not city in result.keys():
            result[city] = {}
        result[city][k] = score_row['{}_score'.format(k)]

Out[1]:
{'Rome': {'adrenaline': 2.11474252688113, 'air': 1.50402325242855},
 'Budapest': {'adrenaline': 1.94859310493828},
 'Ubud': {'adrenaline': 2.65914794847249},
 'Calgary': {'adrenaline': 1.94859310493828},
 'Cancún': {'adrenaline': 3.0},
 'Paris': {'air': 1.50402325242855},
 'New York City': {'air': 1.50402325242855},
 'Abu Dhabi': {'air': 1.50402325242855},
 'Key West': {'air': 1.50402325242855},
 'Salt Lake City': {'air': 1.50402325242855}}