簡體   English   中英

如何在 python 字典中查找每個鍵的唯一值數量

[英]How to find number of unique values per a key in python dictionary

我有一本字典,

{'drink': ["'57 Chevy with a White License Plate",
  "'57 Chevy with a White License Plate",
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '110 in the shade',
  '110 in the shade',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '155 Belmont',
  '155 Belmont',
  '155 Belmont',
  '155 Belmont',
  '24k nightmare',
  '24k nightmare',
  '24k nightmare',
  '24k nightmare',
  '252',
  '252'],
 'ingredient': ['Creme de Cacao',
  'Vodka',
  'Absolut Kurant',
  'Grand Marnier',
  'Grand Marnier',
  'Midori melon liqueur',
  'Malibu rum',
  'Amaretto',
  'Cranberry juice',
  'Pineapple juice',
  'Lager',
  'Tequila',
  'Malibu rum',
  'Light rum',
  '151 proof rum',
  'Dark Creme de Cacao',
  'Cointreau',
  'Milk',
  'Coconut liqueur',
  'Vanilla ice-cream',
  'Dark rum',
  'Light rum',
  'Vodka',
  'Orange juice',
  'Goldschlager',
  'Jägermeister',
  'Rumple Minze',
  '151 proof rum',
  '151 proof rum',
  'Wild Turkey']}

我想找出每種飲料中獨特成分的數量

  1. Drink 57 Chevy with a White License Plate有 2 種獨特的成分,

  2. 飲料1-900-FUK-MEUP有 7 種獨特的成分('Absolut Kurant'、'Grand Marnier'、'Grand Marnier'、'Midori melon liqueur'、'Malibu rum'、'Amaretto'、'Cranberry juice'、'Pineapple果汁')

out_dict = {'drink':['57 Chevy with a White License Plate','1-900-FUK-MEUP'],'unique_count':[2,7]}

您能否寫下您的建議/答案如何完成?

你可以嘗試這樣的事情:

from collections import defaultdict
data = {'drink': ["'57 Chevy with a White License Plate",
  "'57 Chevy with a White License Plate",
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '1-900-FUK-MEUP',
  '110 in the shade',
  '110 in the shade',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '151 Florida Bushwacker',
  '155 Belmont',
  '155 Belmont',
  '155 Belmont',
  '155 Belmont',
  '24k nightmare',
  '24k nightmare',
  '24k nightmare',
  '24k nightmare',
  '252',
  '252'],
 'ingredient': ['Creme de Cacao',
  'Vodka',
  'Absolut Kurant',
  'Grand Marnier',
  'Grand Marnier',
  'Midori melon liqueur',
  'Malibu rum',
  'Amaretto',
  'Cranberry juice',
  'Pineapple juice',
  'Lager',
  'Tequila',
  'Malibu rum',
  'Light rum',
  '151 proof rum',
  'Dark Creme de Cacao',
  'Cointreau',
  'Milk',
  'Coconut liqueur',
  'Vanilla ice-cream',
  'Dark rum',
  'Light rum',
  'Vodka',
  'Orange juice',
  'Goldschlager',
  'Jägermeister',
  'Rumple Minze',
  '151 proof rum',
  '151 proof rum',
  'Wild Turkey']}

result = defaultdict(set)
for drink, ingredient in zip(data['drink'], data['ingredient']):
    result[drink].add(ingredient)

for drink, unique_ingredient in result.items():
    print("{} has {} unique ingredients: {}".format(drink, len(unique_ingredient), list(unique_ingredient)))

Output:

'57 Chevy with a White License Plate has 2 unique ingredients: ['Creme de Cacao', 'Vodka']
1-900-FUK-MEUP has 7 unique ingredients: ['Malibu rum', 'Grand Marnier', 'Cranberry juice', 'Pineapple juice', 'Amaretto', 'Midori melon liqueur', 'Absolut Kurant']
110 in the shade has 2 unique ingredients: ['Lager', 'Tequila']
151 Florida Bushwacker has 8 unique ingredients: ['Milk', 'Malibu rum', 'Vanilla ice-cream', 'Light rum', 'Coconut liqueur', 'Dark Creme de Cacao', 'Cointreau', '151 proof rum']
155 Belmont has 4 unique ingredients: ['Light rum', 'Orange juice', 'Dark rum', 'Vodka']
24k nightmare has 4 unique ingredients: ['Jägermeister', '151 proof rum', 'Rumple Minze', 'Goldschlager']
252 has 2 unique ingredients: ['Wild Turkey', '151 proof rum']

首先,你的結構不合適,讓我們用映射構建一個更好的dict

{drinkKey:ingredientList}
  • 在你的兩個列表之間進行配對('drink', 'ingredient')
  • 將它們分組到第一項,飲料,得到{drink:[('drink', 'ingredient'), ('drink', 'ingredient')]
  • 僅將成分保留在列表中
pairs = list(zip(data['drink'], data['ingredient']))
ingr_per_drink = {k : list(map(itemgetter(1), v)) 
                  for k,v in groupby(sorted(pairs, key=itemgetter(0)), key=itemgetter(0))}
for drink, ingredients in ingr_per_drink.items():
    # whatever you want

感謝您的投入,我在這里完成了它,它符合我的要求。

from collections import OrderedDict
from itertools import groupby
from operator import itemgetter

pairs = list(zip(ex_dict['drink'], ex_dict['ingredient']))

ingr_per_drink = {k : list(map(itemgetter(1), v)) 
                  for k,v in groupby(sorted(pairs, key=itemgetter(0)), key=itemgetter(0))}

drink_counts = {drink:len(set(ingr)) for drink,ingr in ingr_per_drink.items()}

drink_group_sum = {'drink':[],'unique_ingr':[]}

for k, v in drink_counts.items():
    drink_group_sum['drink'].append(k)
    drink_group_sum['unique_ingr'].append(v)

drink_group_sum可以被視為,

{'drink': ["'57 Chevy with a White License Plate",
  '1-900-FUK-MEUP',
  '110 in the shade',
  '151 Florida Bushwacker',
  '155 Belmont',
  '24k nightmare',
  '252'],
 'unique_ingr': [2, 7, 2, 8, 4, 4, 2]}

現在我可以輕松地將這個 dict 傳遞回數據表 Frame 構造函數,以便它將創建一個新的數據表並可以被視為,

Out[4]: 
   | drink                                 unique_ingr
-- + ------------------------------------  -----------
 0 | '57 Chevy with a White License Plate            2
 1 | 1-900-FUK-MEUP                                  7
 2 | 110 in the shade                                2
 3 | 151 Florida Bushwacker                          8
 4 | 155 Belmont                                     4
 5 | 24k nightmare                                   4
 6 | 252                                             2

[7 rows x 2 columns]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM