python-collections

Why is collections.Counter uppercase and collections.defaultdict is not?

こ雲淡風輕ζ 提交于 2021-02-07 20:33:18
问题 Some of the elements in the collections module seem to be uppercase, some other not. Is there a specific rationale behind it? 回答1: According to this reddit comment All classes written in python are upper camel case. All types based on C code are lower. [like the primitives] namedtuple is a function, thus follows the naming convention of functions. deque and defaultdict are types, (C); Counter and OrderedDict are classes, (Python). 来源: https://stackoverflow.com/questions/33636940/why-is

Why is collections.Counter uppercase and collections.defaultdict is not?

佐手、 提交于 2021-02-07 20:32:46
问题 Some of the elements in the collections module seem to be uppercase, some other not. Is there a specific rationale behind it? 回答1: According to this reddit comment All classes written in python are upper camel case. All types based on C code are lower. [like the primitives] namedtuple is a function, thus follows the naming convention of functions. deque and defaultdict are types, (C); Counter and OrderedDict are classes, (Python). 来源: https://stackoverflow.com/questions/33636940/why-is

How to ignore case while doing most_common in Python's collections.Counter?

五迷三道 提交于 2020-01-03 23:03:26
问题 I'm trying to count the number of occurrences of an element in an iterable using most_common in the collections module. >>> names = ['Ash', 'ash', 'Aish', 'aish', 'Juicy', 'juicy'] >>> Counter(names).most_common(3) [('Juicy', 1), ('juicy', 1), ('ash', 1)] But what I expect is, [('juicy', 2), ('ash', 2), ('aish', 2)] Is there a "pythonic" way/trick to incorporate the 'ignore-case' functionality , so that we can get the desired output. 回答1: How about mapping it to str.lower ? >>> Counter(map

Convert 2-column counter-like csv file to Python collections.Counter?

橙三吉。 提交于 2020-01-03 04:22:08
问题 I have a comma separated ( , ) tab delimited ( \t ), file. 68,"phrase"\t 485,"another phrase"\t 43, "phrase 3"\t Is there a simple approach to throw it into a Python Counter ? 回答1: You could use a dictionary comprehension, is considered more pythonic and it can be marginally faster: import csv from collections import Counter def convert_counter_like_csv_to_counter(file_to_convert): with file_to_convert.open(encoding="utf-8") as f: csv_reader = csv.DictReader(f, delimiter="\t", fieldnames=[

Python collections.Counter: most_common complexity

孤街浪徒 提交于 2019-12-28 02:51:04
问题 What is the complexity of the function most_common provided by the collections.Counter object in Python? More specifically, is Counter keeping some kind of sorted list while it's counting, allowing it to perform the most_common operation faster than O(n) when n is the number of (unique) items added to the counter? For you information, I am processing some large amount of text data trying to find the n-th most frequent tokens. I checked the official documentation and the TimeComplexity article

python counting elements in iterable with filter

浪尽此生 提交于 2019-12-20 06:23:26
问题 To count the elements in a list, you can use collections.Counter, but what if only some of the elements have to be counted? I've set up this example (please note: numpy is just for convenience. In general the list will contain arbitrary python objects): num_samples = 10000000 num_unique = 1000 numbers = np.random.randint(0, num_unique, num_samples) I would like to count how often a number occurs in this list, but I'm only interested in numbers <= 10. This is the baseline to beat. The Counter

collections.Counter: most_common INCLUDING equal counts

人盡茶涼 提交于 2019-12-20 01:47:05
问题 In collections.Counter , the method most_common(n) returns only the n most frequent items in a list. I need exactly that but I need to include the equal counts as well. from collections import Counter test = Counter(["A","A","A","B","B","C","C","D","D","E","F","G","H"]) -->Counter({'A': 3, 'C': 2, 'B': 2, 'D': 2, 'E': 1, 'G': 1, 'F': 1, 'H': 1}) test.most_common(2) -->[('A', 3), ('C', 2) I would need [('A', 3), ('B', 2), ('C', 2), ('D', 2)] since they have the same count as n=2 for this case.

collections.Counter: most_common INCLUDING equal counts

筅森魡賤 提交于 2019-12-20 01:46:38
问题 In collections.Counter , the method most_common(n) returns only the n most frequent items in a list. I need exactly that but I need to include the equal counts as well. from collections import Counter test = Counter(["A","A","A","B","B","C","C","D","D","E","F","G","H"]) -->Counter({'A': 3, 'C': 2, 'B': 2, 'D': 2, 'E': 1, 'G': 1, 'F': 1, 'H': 1}) test.most_common(2) -->[('A', 3), ('C', 2) I would need [('A', 3), ('B', 2), ('C', 2), ('D', 2)] since they have the same count as n=2 for this case.

Python Counter Comparison as Bag-type

女生的网名这么多〃 提交于 2019-12-12 11:10:00
问题 I need a bag/multiset-like data type in Python. I understand collections.Counter is often used for this purpose. But the comparison operators don't seem to work: In [1]: from collections import Counter In [2]: bag1 = Counter(a=1, b=2, c=3) In [3]: bag2 = Counter(a=2, b=2) In [4]: bag1 > bag2 Out[4]: True This seems like a bug to me. I expected the less-than and greater-than operators to perform set-like subset and superset comparisons. But if that were the case then bag1 > bag2 would be false

Why doesn't OrderedDict use super?

半城伤御伤魂 提交于 2019-12-05 00:51:37
问题 We can create an OrderedCounter trivially by using multiple inheritance: >>> from collections import Counter, OrderedDict >>> class OrderedCounter(Counter, OrderedDict): ... pass ... >>> OrderedCounter('Mississippi').items() [('M', 1), ('i', 4), ('s', 4), ('p', 2)] Correct me if I'm wrong, but this crucially relies on the fact that Counter uses super: class Counter(dict): def __init__(*args, **kwds): ... super(Counter, self).__init__() ... That is, the magic trick works because >>>