简体   繁体   English

Python Counter:计数为x的打印键

[英]Python Counter: print key whose count is x

Say I have a Counter object representing a collection of words: 假设我有一个Counter对象代表一组单词:

>>> words = ['hello', 'hello', 'hello', 'world']
>>> counter = Counter(words)

One way to find out which words have count 1 would be to iterate over counter : 找出哪些单词计数为1的一种方法是迭代counter

for word, count in counter.items():
    if count == 1:
        print(word)

Is there an easier/better way to do this? 有没有更容易/更好的方法来做到这一点? That is, can one "invert" counter to give the word whose count is x ? 也就是说,一个“反转” counter可以给出计数为x的单词吗?

To reverse any mapping—whether a Counter , a dict , or anything else: 要反转任何映射 - 无论是Counterdict还是其他任何东西:

rev = {v: k for k, v in d.items()}

Then you use that like any other dictionary: 然后你像任何其他字典一样使用它:

key_whose_count_is_10 = rev[10]

In the case where there are two keys with the same value, the value will map to one of them, arbitrarily. 在有两个具有相同值的键的情况下,该值将任意映射到其中一个键。 But that's pretty much inherent in your problem. 但这在您的问题中几乎是固有的。 You're asking for "the" key whose count is x ; 你要的是那个计数为x的“钥匙”; what do you want to do if there are three keys whose count is x ? 如果有三个按键计数为x你想做什么?


If you're only going to be making one query, rather than multiple queries, it's more efficient to just iterate. 如果您只打算进行一次查询,而不是多次查询,那么迭代会更有效。 Which one is clearer (which is almost always more important) is arguable. 哪一个更清楚 (几乎总是更重要)是有争议的。 Here's one way to do it, for comparison: 这是一种方法,用于比较:

key_whose_count_is_10 = next(k for k, v in d.items() if v==10)

It would be far better to put every element with value 1 in a list, I think. 我认为,将每个元素值1放在列表中要好得多。 Here's a Pythonic way to do this: 这是一个Pythonic方法:

new_list = [w for w in words if counter[w] == 1]

Like this, you will store every word in words that has value of 1 in your counter. 这样,你将存储在每一个词words已经在你的计数器的值1。

So, for example, if you have in your list another string, say the string test : 因此,例如,如果您在列表中有另一个字符串,请说出字符串test

words = ['hello', 'hello', 'hello', 'world', 'test']

then, the new list will have the values world and test . 然后,新列表将具有值worldtest

Your Counter object uses each word as the key and stores the number of occurrences as the value. Counter对象使用每个单词作为键,并将出现次数存储为值。

To do what you want to do, you need to use the number of occurrences as the key and the list of words as the value: 要执行您想要执行的操作,您需要使用出现次数作为键,将单词列表用作值:

wordDict = {}
for word, count in counter.items():
    if count in wordDict:
        wordDict[count].append(word)
    else:
        wordDict[count] = [word]

Then you can use wordDict[2] to grab the list of words that appear twice. 然后你可以使用wordDict[2]来获取出现两次的单词列表。

You can use a list comprehension to check each element's count 您可以使用列表推导来检查每个元素的计数

>>> words = ['hello', 'hello', 'hi', 'hi', 'world', 'foo', 'bar']
>>> from collections import Counter
>>> counter = Counter(words)
>>> [i for i in counter if counter[i] == 1]
['world', 'bar', 'foo']

You can also use the count() function on the original list 您还可以在原始列表中使用count()函数

>>> [i for i in words if words.count(i) == 1]
['world', 'foo', 'bar']

You can use defaultdict : 你可以使用defaultdict

import collections
d = collections.defaultdict(list)
for word, count in counter.items():
    d[count].append(word)

You can then do: 然后你可以这样做:

d[1]

to get all words with count one (as there can be one or more words). 获得计数一的所有单词(因为可以有一个或多个单词)。

Using filter (or ifilter if itertools is imported) 使用过滤器(如果导入了itertools,则使用ifilter)

n = 1
for word in filter(lambda w: counter[w] == n, words):
  print word

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

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