正如你可以在這裏看到的,我有一個「主」字典,其中每個值都是字典本身。現在我想比較主要字典'(可以超過2)「名稱」值,例如「DE,斯圖加特」與「DE,德累斯頓」和X並且只有唯一的「名稱」值剩下。Python - 如何比較多個字典並刪除重複的值?
我知道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": [
],
這是什麼語法? –
'null'不是python代碼。這是JSON文本? –
@ Jean-FrançoisFabre是的,我很確定它是 –