简体   繁体   English

如何在python字典中获取与一个值相关联的多个键

[英]How to get multiple keys assoicated with one value in python dictionary

#mode of numbers
number_counts = {}
for number in numbers:
    if number in number_counts:
        number_counts[number] += 1
    else:
        number_counts[number] = 1

max_count=0
for number in number_counts:
    if number_counts[number] > max_count:
        max_count = number_counts[number]
        print('Mode: ', number)

        #allkeys = ''
        #if number_counts[number] == max_count:
            #allnumbers = allnumbers +" "+str(number)+","
            #print('Mode: ', [allnumbers])

Here is the section of code that I am trying to fix. 这是我要修复的代码部分。 I am trying to find the mode of a list of numbers (coded as numbers earlier in my program). 我试图找到数字列表的模式(在程序的前面编码为数字)。 I have got it to give me one mode, but not all of them. 我有办法给我一种模式,但不是全部。 I commented out what I thought might on the right track based on other posts. 我根据其他帖子评论了我认为可能在正确的轨道上所做的事情。

Your program prints a number as a mode only when the value of max_count is changed, which itself happens only when it finds a key with a larger count. 您的程序仅在更改max_count的值时才将数字作为模式输出,这种情况仅在找到数量更大的键时才会发生。 If a key with the same count is encountered, it is ignored and you never see it printed. 如果遇到具有相同计数的键,它将被忽略并且您永远不会看到它被打印。 This is due to the line if number_counts[number] > max_count: --it should be if number_counts[number] >= max_count: . 这是由于if number_counts[number] > max_count:if number_counts[number] > max_count: -- if number_counts[number] >= max_count:

However, your routine has another problem: it prints possible modes before it is sure they are modes--a number is printed if it has the largest count so far , not the largest count overall. 但是,您的例程还有另一个问题:在确定可能的模式之前,它会打印可能的模式-如果数字到目前为止最大,而不是总体上最大,则会打印一个数字。

So change your final section that tries to print the mode into two sections (the code starting with max_count=0 ). 因此,将试图将模式打印为两部分的最后一部分更改为(以max_count=0开头的代码)。 The first part finds the value of max_count but cannot be sure what that is until the first section is finished. 第一部分找到max_count的值,但是直到第一部分完成后才能确定那是什么。 The second part then finds all numbers with that count and prints them. 然后,第二部分查找具有该计数的所有数字并打印出来。

By the way, that first part can be done in just one line: 顺便说一下,第一部分只需一行即可完成:

max_count = max(number_counts.values())

There are other ways to find the modes, but this seems to be an exercise for you. 还有其他找到模式的方法,但这似乎是您的练习。

If you are simply trying to one mode, that's relatively easy. 如果您只是尝试一种模式,那是相对容易的。

from collections import Counter

A = [0, 1, 2, 3, 4, 1, 1]
counts = Counter(A)

(mode, count) = counts.most_common(1)[0]

If you are really trying to map a dict of values, you would probably do something like. 如果您确实试图映射值的字典,则可能会执行类似的操作。

from collections import defaultdict

counts = {
    0: 1,
    1: 1,
    2: 3,
    3: 1,
}

results = defaultdict(list)
for (number, value) in counts.items():
    results[value].append(number)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM