如何查找具有单个键的多个值的字典

时间:2019-09-01 19:24:48

标签: python csv dictionary

我有两个不同的字典:

  • 显示品牌名称及其类型(例如,Air是品牌,而鞋子是type)

D1 = {'apple': 'phone', 'samsung': 'phone', 'LG': 'TV', 'sony': 'TV'}

  • 这将显示产品名称和相同订单的数量。

D2 = {'apple': 3, 'samsung': 5, 'LG': 1, 'sony': 2}

我要创建一个新词典(不使用熊猫),以使其显示给定类型最受欢迎的品牌以及相同订单的数量。最受欢迎是指总订单最多的品牌。如果两个或多个品牌(具有给定类型)的订单数量相同,则选择任何人。

我试图合并两个字典,如下所示:

{'samsung': ['phone', 5], 'apple': ['phone', 3], 'sony': ['TV', 2], 'LG': ['TV', 1]}

我不确定这是否是正确的方法。如果是这样,如何从该词典中获取所需的数据?

我用来将两个词典合并为一个的代码:

d3={}
for key in (d2:keys()|d1.keys()):
  if key in d2: d3.setdefault(key, []).append(d2[key])
  if key in d1: d3.setdefault(key, []).append(d1[key])

预期结果:-

{'samsung': 5, 'sony': 2}

4 个答案:

答案 0 :(得分:1)

这可能有点复杂,但这应该可以工作:

ion-toolbar {
  --background-color: transparent;
  --ion-color-base: transparent !important;
}

它打印:

  

{'sony':2,'samsung':5}

说明:第一个循环为每个类别创建一个词典,列表中的品牌为值。第二个遍历所有类别并找到最大值的那个。特别是第二个循环有点冗长...

答案 1 :(得分:1)

参加聚会有点晚,但我认为是迄今为止最简单的事情

D1 = {'apple': 'phone', 'samsung': 'phone', 'LG': 'TV', 'sony': 'TV'}
D2 = {'apple': 3, 'samsung': 5, 'LG': 1, 'sony': 2}

most_popular = {}

for brand, type_ in D1.items():
    orders = D2[brand]
    current_winner = most_popular.get(type_, None)
    if current_winner is None or orders > current_winner[1]:
        most_popular[type_] = (brand, orders)

result = {brand: orders for type_, (brand, orders) in most_popular.items()}

print(result)
# {'samsung': 5, 'sony': 2}

对于D1中的每个品牌类型对,它都会检查该产品类型是否没有条目(在这种情况下,我们将其放入)在订单上超过当前条目(在这种情况下,我们将使用新品牌更新条目)。

最后一行将这些数据转换为结果的正确格式。


编辑:

设法将其压平成非常脏的单线(不计算most_popular的初始分配):

D1 = {'apple': 'phone', 'samsung': 'phone', 'LG': 'TV', 'sony': 'TV'}
D2 = {'apple': 3, 'samsung': 5, 'LG': 1, 'sony': 2}

most_popular = {}
result = {brand: orders for type_, (brand, orders) in {type_: (brand, D2[brand]) for brand, type_ in D1.items() if not most_popular.get(type_, None) or D2[brand] > most_popular.get(type_, None)[1]}.items()}

print(result)
# {'samsung': 5, 'sony': 2}

答案 2 :(得分:0)

哦,看来我来晚了。 :P

D1 = {'apple': 'phone', 'samsung': 'phone', 'LG': 'TV', 'sony': 'TV'}
D2 = {'apple': 3, 'samsung': 5, 'LG': 1, 'sony': 2}

#creates list of lists
mylist = [list(x) for x in zip(D1,D1.values())]
#adds thrid value to lists in list (source countities in D2)
mylist = [x + [D2.get(x[0])] for x in mylist]

#creates empty result dictionary
result = {}
#loops through unique value in dictionary on index position 1 (phone, tv's etc.)
for element in list(set([x[1] for x in mylist])):
    #temporary saves current winner
    current_max = 0
    current_winner = ""
    #loops through the lists in the list of lists
    for sublist in mylist:
        #groups the lists by skipping lists which are not in the current category(e.g. TVs)
        if sublist[1]==element:
            #checks if the current lists has a higher maximum value than the currently best
            if sublist[2]>current_max:
                #if yes, it replaces the current winner in the group
                current_max = sublist[2]
                current_winner = sublist[0]
    #adds the current winner to the result dictionary, then starts looking for the winner in the next group
    result[current_winner] = current_max

#prints result
print(result)

答案 3 :(得分:0)

此代码将为您服务。我在d3中做了一些修改

d1 = {'apple': 'phone', 'samsung': 'phone', 'LG': 'TV', 'sony': 'TV'}
d2 = {'apple': 3, 'samsung': 5, 'LG': 1, 'sony': 2}

d3 = {}
for x in d1.keys():
    if(d1[x] in d3):
        d3[d1[x]].append(x)
    else:
        d3[d1[x]] = list()
        d3[d1[x]].append(x) 

MAX = {}
for brands in d3.keys():
    maximum = 0
    bName = ""
    for brand in d3[brands]:
        if(d2[brand]>maximum):
            maximum = d2[brand]
            bName = brand
    MAX[bName] = d2[bName]

print(MAX)