正如你在下面看到的,我有一个“主”字典,其中每个值都是一个dict本身。现在我要比较主词的(可以超过2)“name”值,例如“DE,Stuttgart”与“DE,Dresden”和X,只剩下唯一的“name”值。你知道吗
例如,我知道x for x in y if x['key'] != None
结构,但据我所知,我只能用它来过滤单个字典。你知道吗
输入:
"DE, Stuttgart": [
{
"url": "http://twitter.com/search?q=%23ISIS",
"query": "%23ISIS",
"tweet_volume": 21646,
"name": "#ISIS",
"promoted_content": null
},
{
"url": "http://twitter.com/search?q=%22Hans+Rosling%22",
"query": "%22Hans+Rosling%22",
"tweet_volume": 44855,
"name": "Hans Rosling",
"promoted_content": null
},
{
"url": "http://twitter.com/search?q=%22Betsy+DeVos%22",
"query": "%22Betsy+DeVos%22",
"tweet_volume": 664741,
"name": "Betsy DeVos",
"promoted_content": null
},
{
"url": "http://twitter.com/search?q=Nioh",
"query": "Nioh",
"tweet_volume": 24160,
"name": "Nioh",
"promoted_content": null
},
{
"url": "http://twitter.com/search?q=%23FCBWOB",
"query": "%23FCBWOB",
"tweet_volume": 14216,
"name": "#FCBWOB",
"promoted_content": null
},
{
"url": "http://twitter.com/search?q=%23sid2017",
"query": "%23sid2017",
"tweet_volume": 28277,
"name": "#sid2017",
"promoted_content": null
}
],
"DE, Dresden": [
{
"url": "http://twitter.com/search?q=%22Hans+Rosling%22",
"query": "%22Hans+Rosling%22",
"tweet_volume": 44855,
"name": "Hans Rosling",
"promoted_content": null
},
{
"url": "http://twitter.com/search?q=%22Betsy+DeVos%22",
"query": "%22Betsy+DeVos%22",
"tweet_volume": 664741,
"name": "Betsy DeVos",
"promoted_content": null
},
{
"url": "http://twitter.com/search?q=Nioh",
"query": "Nioh",
"tweet_volume": 24160,
"name": "Nioh",
"promoted_content": null
},
{
"url": "http://twitter.com/search?q=%23FCBWOB",
"query": "%23FCBWOB",
"tweet_volume": 14216,
"name": "#FCBWOB",
"promoted_content": null
},
{
"url": "http://twitter.com/search?q=%23sid2017",
"query": "%23sid2017",
"tweet_volume": 28277,
"name": "#sid2017",
"promoted_content": null
}
],
输出:
"DE, Stuttgart": [
{
"url": "http://twitter.com/search?q=%23ISIS",
"query": "%23ISIS",
"tweet_volume": 21646,
"name": "#ISIS",
"promoted_content": null
}
],
"DE, Dresden": [
],
假设
d1
和d2
是你的两本字典。您可以通过以下方式获取d1
中不在d2
中的键的列表:您可以将名称收集到^{} ,然后重建原始dict,同时只保留那些具有唯一名称的子dict:
输出:
这将为任意数量的位置输出所需的dict。请注意,@niemmi的解决方案效率更高:
相关问题 更多 >
编程相关推荐