2017-03-01 90 views
0

正如你可以在這裏看到的,我有一個「主」字典,其中每個值都是字典本身。現在我想比較主要字典'(可以超過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": [ 
    ], 
+0

這是什麼語法? –

+0

'null'不是python代碼。這是JSON文本? –

+0

@ Jean-FrançoisFabre是的,我很確定它是 –

回答

3

你可以收集名稱Counter然後重建原始字典,而僅僅保留了其具有獨特的名稱的子類型的字典:

main = { 
    "DE, Stuttgart": [ 
     { 
      "url": "http://twitter.com/search?q=%23ISIS", 
      "query": "%23ISIS", 
      "tweet_volume": 21646, 
      "name": "#ISIS", 
      "promoted_content": None 
     }, 
     { 
      "url": "http://twitter.com/search?q=%22Hans+Rosling%22", 
      "query": "%22Hans+Rosling%22", 
      "tweet_volume": 44855, 
      "name": "Hans Rosling", 
      "promoted_content": None 
     }, 
     { 
      "url": "http://twitter.com/search?q=%22Betsy+DeVos%22", 
      "query": "%22Betsy+DeVos%22", 
      "tweet_volume": 664741, 
      "name": "Betsy DeVos", 
      "promoted_content": None 
     }, 
     { 
      "url": "http://twitter.com/search?q=Nioh", 
      "query": "Nioh", 
      "tweet_volume": 24160, 
      "name": "Nioh", 
      "promoted_content": None 
     }, 
     { 
      "url": "http://twitter.com/search?q=%23FCBWOB", 
      "query": "%23FCBWOB", 
      "tweet_volume": 14216, 
      "name": "#FCBWOB", 
      "promoted_content": None 
     }, 
     { 
      "url": "http://twitter.com/search?q=%23sid2017", 
      "query": "%23sid2017", 
      "tweet_volume": 28277, 
      "name": "#sid2017", 
      "promoted_content": None 
     } 
    ], 
    "DE, Dresden": [ 
     { 
      "url": "http://twitter.com/search?q=%22Hans+Rosling%22", 
      "query": "%22Hans+Rosling%22", 
      "tweet_volume": 44855, 
      "name": "Hans Rosling", 
      "promoted_content": None 
     }, 
     { 
      "url": "http://twitter.com/search?q=%22Betsy+DeVos%22", 
      "query": "%22Betsy+DeVos%22", 
      "tweet_volume": 664741, 
      "name": "Betsy DeVos", 
      "promoted_content": None 
     }, 
     { 
      "url": "http://twitter.com/search?q=Nioh", 
      "query": "Nioh", 
      "tweet_volume": 24160, 
      "name": "Nioh", 
      "promoted_content": None 
     }, 
     { 
      "url": "http://twitter.com/search?q=%23FCBWOB", 
      "query": "%23FCBWOB", 
      "tweet_volume": 14216, 
      "name": "#FCBWOB", 
      "promoted_content": None 
     }, 
     { 
      "url": "http://twitter.com/search?q=%23sid2017", 
      "query": "%23sid2017", 
      "tweet_volume": 28277, 
      "name": "#sid2017", 
      "promoted_content": None 
     } 
    ] 
} 
from collections import Counter 
import pprint 

names = Counter(d['name'] for l in main.values() for d in l) 
result = {k: [d for d in v if names[d['name']] == 1] for k, v in main.items()} 

pprint.pprint(result) 

輸出:

{'DE, Dresden': [], 
'DE, Stuttgart': [{'name': '#ISIS', 
        'promoted_content': None, 
        'query': '%23ISIS', 
        'tweet_volume': 21646, 
        'url': 'http://twitter.com/search?q=%23ISIS'}]} 
+0

不錯,乾淨,簡單。做得好! –

+0

工作得很好。我現在在主字典中總共有15個字,它的運行速度非常快,並且和預期的一樣。 –

0

假設d1d2是你的兩個庫。你可以得到的d1鍵列表中沒有的d2有:

[k for k in d if k not in d2] 
1

這將輸出所需的字典,對於任何數量的位置。需要注意的是@ niemmi的解決方案是有效得多:

main_dict = {"DE, Stuttgart": [ 
    { 
     "url": "http://twitter.com/search?q=%23ISIS", 
     "query": "%23ISIS", 
     "tweet_volume": 21646, 
     "name": "#ISIS", 
     "promoted_content": None 
    }, 
    { 
     "url": "http://twitter.com/search?q=%22Hans+Rosling%22", 
     "query": "%22Hans+Rosling%22", 
     "tweet_volume": 44855, 
     "name": "Hans Rosling", 
     "promoted_content": None 
    }, 
    { 
     "url": "http://twitter.com/search?q=%22Betsy+DeVos%22", 
     "query": "%22Betsy+DeVos%22", 
     "tweet_volume": 664741, 
     "name": "Betsy DeVos", 
     "promoted_content": None 
    }, 
    { 
     "url": "http://twitter.com/search?q=Nioh", 
     "query": "Nioh", 
     "tweet_volume": 24160, 
     "name": "Nioh", 
     "promoted_content": None 
    }, 
    { 
     "url": "http://twitter.com/search?q=%23FCBWOB", 
     "query": "%23FCBWOB", 
     "tweet_volume": 14216, 
     "name": "#FCBWOB", 
     "promoted_content": None 
    }, 
    { 
     "url": "http://twitter.com/search?q=%23sid2017", 
     "query": "%23sid2017", 
     "tweet_volume": 28277, 
     "name": "#sid2017", 
     "promoted_content": None 
    } 
], 
"DE, Dresden": [ 
    { 
     "url": "http://twitter.com/search?q=%22Hans+Rosling%22", 
     "query": "%22Hans+Rosling%22", 
     "tweet_volume": 44855, 
     "name": "Hans Rosling", 
     "promoted_content": None 
    }, 
    { 
     "url": "http://twitter.com/search?q=%22Betsy+DeVos%22", 
     "query": "%22Betsy+DeVos%22", 
     "tweet_volume": 664741, 
     "name": "Betsy DeVos", 
     "promoted_content": None 
    }, 
    { 
     "url": "http://twitter.com/search?q=Nioh", 
     "query": "Nioh", 
     "tweet_volume": 24160, 
     "name": "Nioh", 
     "promoted_content": None 
    }, 
    { 
     "url": "http://twitter.com/search?q=%23FCBWOB", 
     "query": "%23FCBWOB", 
     "tweet_volume": 14216, 
     "name": "#FCBWOB", 
     "promoted_content": None 
    }, 
    { 
     "url": "http://twitter.com/search?q=%23sid2017", 
     "query": "%23sid2017", 
     "tweet_volume": 28277, 
     "name": "#sid2017", 
     "promoted_content": None 
    } 
] 
} 

def get_names(main_dict, location): 
    return {small_dict["name"] for small_dict in main_dict[location]} 

def get_names_from_other_locations(main_dict, location): 
    other_locations = [other_loc for other_loc in main_dict if other_loc != location] 
    return {small_dict["name"] for other_location in other_locations for small_dict in main_dict[other_location]} 

def get_uniq_names(main_dict, location): 
    return get_names(main_dict, location) - get_names_from_other_locations(main_dict, location) 

def get_dict(main_dict, location, name): 
    for small_dict in main_dict[location]: 
     if small_dict["name"] == name: 
      return small_dict 
    return None 

print {location: [get_dict(main_dict,location,uniq_name) for uniq_name in get_uniq_names(main_dict, location)] for location in main_dict } 
# {'DE, Stuttgart': [{'url': 'http://twitter.com/search?q=%23ISIS', 'query': '%23ISIS', 'tweet_volume': 21646, 'name': '#ISIS', 'promoted_content': None}], 'DE, Dresden': []} 
+0

我得到了該功能的功能,但在我有3個字符的那一刻,這是否仍然有效? –

+0

更新了答案。 –

+0

嗨Eric,謝謝你的幫忙!正如您在編輯中所寫的,我使用@niemmi的解決方案,但非常感謝。我之前並不知道你可以在Python中「減去」字典。 –