2017-03-01 1 views
0

ここからわかるように、私は、各値が単独で辞書である「メイン」辞書を持っています。ここでは、主題辞書の(2以上あります) "name"の値を互いに比較したいので、例えば "DE、Stuttgart"と "DE、Dresden"とXを持ち、ユニークな "name"値左。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に名前を収集して、固有の名前を持っているもののみサブdicts維持しながら、オリジナルの辞書を再構築することができます:

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があなたの2つの辞書ですとしましょう。

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

この意志出力希望のdict、場所の任意の数のために:あなたはd2にしていないd1のキーのリストを取得することができます。その@の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つのdictsを持っている瞬間に、これはまだ動作しますか? –

+0

答えを更新しました。 –

+0

エリック、助けてくれてありがとう!あなたがあなたの編集で書いたように、私は@ニームミによる解決策に行きましたが、ありがとうございます。私はあなたがPythonでdictsを単に "差し引く"ことができることを以前は知らなかった。 –

関連する問題