2016-12-31 5 views
-1

果物と色を要約したいと思います。そこ果物とそれに対応する色でタプルのリストがあり、私は黄色の色の果物をカウントしたい、そして、辞書を構築する:タプルのリスト内の2つの項目を辞書としてグループ化するにはどうすればよいですか?

yellowfruit= { 'banana': 1, 'grape' : 2, 'orange': 2, 'peach': '4', 'pear':1 } 

次はフルーツ

fruit= [ 
       ('apple', 'green'), 
       ('apple', 'red'), 
       ('banana', 'yellow'), 
       ('grape', 'green'), 
       ('grape', 'yellow'), 
       ('grape', 'yellow'), 
       ('grape', 'red'), 
       ('orange', 'yellow'), 
       ('orange', 'yellow'), 
       ('mango', 'green'), 
       ('peach', 'yellow'), 
       ('peach', 'red'), 
       ('peach', 'yellow'), 
       ('peach', 'yellow'), 
       ('peach', 'red'), 
       ('peach', 'yellow'), 
       ('peach', 'red'), 
       ('pear', 'yellow'), 
      ] 
の情報であり、

これらは、コメントと私のコードです:

fruit= [ 
     ('apple', 'green'), 
     ('apple', 'red'), 
     ('banana', 'yellow'), 
     ('grape', 'green'), 
     ('grape', 'yellow'), 
     ('grape', 'yellow'), 
     ('grape', 'red'), 
     ('orange', 'yellow'), 
     ('orange', 'yellow'), 
     ('mango', 'green'), 
     ('peach', 'yellow'), 
     ('peach', 'red'), 
     ('peach', 'yellow'), 
     ('peach', 'yellow'), 
     ('peach', 'red'), 
     ('peach', 'yellow'), 
     ('peach', 'red'), 
     ('pear', 'yellow'), 
    ] 

yellowfruit = { } # create an empty dictionary 
fruitname = fruit[0][0] # 'apple' is the first fruit 
for i in range(len(fruit)): # loop over the tuples in the fruit list 

    if fruit[i][0] == fruitname: 
     if fruit[i][1] == 'yellow': 
     # for the same kind of fruit, if its colour is yellow, count update for 1 
      n += 1 
     else: # if the same kind of fruit but not the colour of yellow 
      continue 

    else: 
     n = 1 # if not the same kind of fruit, refill the count as 1 
     fruitname = fruit[i][0] # if the fruit change, always update the current item as the fruit name 
    yellowfruit[fruitname] = n # create the dictionary 

print(yellowfruit) 

結果:

{'peach': 4, 'banana': 1, 'orange': 2, 'grape': 3, 'pear': 1, 'mango': 1} 

何が問題ですか?

+0

(F果物でF、Cのためであれば、C == '黄色')'あなたはで始まる – vaultah

+2

'N = 1' *それが黄色だかどうか*。また、なぜ異なる出力が期待されるのか説明すると参考になりますので、読者はそれ自体を比較する必要はありません。 – jonrsharpe

+1

@jonrsharpe私はif文を 'fruit [i] [0] == fruitnameとfruit [i] [1] ==' yellow ':'のように変更しても動作しません。 – user5802211

答えて

2

タプルは果物でソートされていますが、必ずしもそうでない場合もあります。フルーツが辞書に存在するか存在しないかの2つのケースを処理するには、(fruityellow_fruitにある場合)または0(存在しない場合)のいずれかを返すyellow_fruit.get(fruit, 0)を使用します。辞書には順序がないので、プログラムを再実行すると、印刷されたキーと値のペアが置換されます。あなただけの `collections.Counterにしてください

tuple_list = [ 
    ('apple', 'green'), 
    ('apple', 'red'), 
    ('banana', 'yellow'), 
    ('grape', 'green'), 
    ('grape', 'yellow'), 
    ('grape', 'yellow'), 
    ('grape', 'red'), 
    ('orange', 'yellow'), 
    ('orange', 'yellow'), 
    ('mango', 'green'), 
    ('peach', 'yellow'), 
    ('peach', 'red'), 
    ('peach', 'yellow'), 
    ('peach', 'yellow'), 
    ('peach', 'red'), 
    ('peach', 'yellow'), 
    ('peach', 'red'), 
    ('pear', 'yellow'), 
] 

yellow_fruit = {} 
for fruit, colour in tuple_list: 
    if colour == 'yellow': 
     yellow_fruit[fruit] = yellow_fruit.get(fruit, 0) + 1 
print(yellow_fruit) # {'banana': 1, 'orange': 2, 'grape': 2, 'peach': 4, 'pear': 1} 
+0

ありがとうございました!私の問題は解決され、あなたはdict.get()がとても役立つことを私に知らせる! – user5802211

関連する問題