2016-08-08 12 views
1

カテゴリのある単語の辞書があります。私は単語が私のリストの言葉と一致すればカテゴリを出力したい。リストに一致する辞書(Python)

dictionary = { 
    "object" : 'hat', 
    "animal" : 'cat', 
    "human" : 'steve' 
} 

list_of_things = ["steve", "tom", "cat"] 

for categories,things in dictionary.iteritems(): 
    for stuff in list_of_things: 

     if stuff in things: 
      print categories 
     if stuff not in things: 
      print "dump as usual" 

は、現在の出力は次のようになります:これは私のコードは、現時点では次のようになります

dump as usual 
dump as usual 
dump as usual 
human 
dump as usual 
dump as usual 
dump as usual 
dump as usual 
animal 

しかし、私は、出力は次のようになりたい:

human 
dump as usual 
animal 

I私のリストがそれが辞書で繰り返すすべてのものを印刷するのを望まない。私は一致する言葉を印刷するだけです。どうすればいい?

+1

あなたは、辞書、または単にリストを反復することを意図していますか? – James

答えて

1

まず、あなたの辞書は構成が不十分です。そのキーと値を入れ替えたようです。カテゴリをキーとして使用することで、カテゴリごとに1つのオブジェクトしか作成できません。これはおそらくあなたが望むものではありません。また、一般的に悪い兆候である項目を検索するためには、辞書内のすべての項目を読み取らなければならないということも意味します。このための修正は簡単です:アイテムをコロンの左側に置き、カテゴリーを右側に置きます。次に、 'in'演算子を使用して簡単に辞書を検索できます。

あなたが直接質問している限り、最初にlist_of_thingsをループし、それぞれを辞書と照合して結果を出力する必要があります。これは、リスト内の項目ごとに1つだけ表示されます。

dictionary = { 
    'hat' : 'object', 
    'cat' : 'animal', 
    'steve' : 'human' 
} 

list_of_things = ['steve', 'tom', 'cat'] 

for thing in list_of_things: 
    if thing in dictionary: 
     print dictionary[thing] 
    else: 
     print "dump as usual" 

この出力:

human 
dump as usual 
animal 
+0

とてもシンプルでとても効果的です!どうもありがとうございます! – semiflex

+0

大歓迎です。 Pythonは、データのコレクションをエレガントに処理する上で非常に優れているため、そのデータの構造は非常に重要です。 –

1

あなたがTrueに偽(カテゴリが見つからない)から変更するインナーforループ、(カテゴリが発見された)の内側にブール値を使用して、唯一のforループif boolean = False:

最後categoriesを印刷することができ
isMatchFound = False 
for categories, things in dictionary.iteritems(): 
    isMatchFound = False 
    for stuff in list_of_things: 
     if stuff in things: 
      isMatchFound = True 
      print stuff 
    if isMatchFound == False: 
     print("dump as usual") 
+0

Urrブール値をどこに入れるのですか?ごめんなさい!私は完全な騒ぎです! – semiflex

+0

@semiflex上記の編集を参照してください – Frangipanes

1

あなたの実際のデータはあなたの例のように見えるどのくらいに応じて、

category_thing= { 
    "object" : 'hat', 
    "animal" : 'cat', 
    "human" : 'steve' 
} 

list_of_things = ["steve", "tom", "cat"] 
thingset=set(list_of_things) # just for speedup 

for category,thing in category_thing.iteritems(): 

     if thing in thingset: 
      print category 
     else: 
      print "dump as usual" 

やあなたのマッピングが本当にとしてシムであれば行うことができますあなたが行うことができるあなたの例のように

category_thing= { 
    "object" : 'hat', 
    "animal" : 'cat', 
    "human" : 'steve' 
} 

thing_category=dict((t,c) for c,t in category_thing.items()) # reverse the dict - if you have duplicate values (things), you should not do this 

list_of_things = ["steve", "tom", "cat"] 

for stuff in list_of_things: 
    msg=thing_category.get(stuff,"dump as usual") 
    print msg 
+1

これは 'category_thing'の重複した値を失うことになります –

+0

@KrishnachandraSharmaはい、より安全なソリューションを追加しました – janbrohl

0

出力に3行しか必要ないので、for-loopsを並べ替える必要があります。

for stuff in list_of_things: 
    print_val = None 
    for categories,things in dictionary.iteritems(): 
    if stuff in things: 
     print_val=categories 
    if print_val is None: 
    print_val="dump as usual" 
    print print_val 
関連する問題