About 64,000 results
Open links in new tab
  1. python - How do I count the occurrences of a list item? - Stack …

    Apr 8, 2010 · Counter in Python 2 was a little on the slow side, yes. It uses C-optimised code to do the counting in ...

  2. python - Using a dictionary to count the items in a list - Stack …

    Sep 12, 2019 · I think this may be preferable to using count(), because it will only go over the iterable once, whereas count may search the entire thing on every iteration. I used this method to parse many megabytes of statistical data and it always was reasonably fast.

  3. How to sort Counter by value? - python - Stack Overflow

    Jan 6, 2014 · Other than doing list comprehensions of reversed list comprehension, is there a pythonic way to sort Counter by value? If so, it is faster than this: >>> from collections import Counter &...

  4. python - Formatting output of Counter - Stack Overflow

    category = Counter(category_list) print category the following is the output, Counter({'a': 8508, 'c': 345, 'w': 60}) I have to display the above result as follows, a 8508 c 345 w 60 I tried to iterate over the counter object but I'm unsuccessful. Is there a way to …

  5. Using Counter() in Python to build histogram? - Stack Overflow

    Oct 5, 2013 · Based on Igonato's answer I created a helper module 'plot' with a class 'Plot'. It has two functions hist() and barchart() two show Igonato's approach as well as using the matplotlib hist functionality directly as might haven been originally intended in the question.

  6. python - How to find the count of a word in a string - Stack …

    You can use the Python regex library re to find all matches in the substring and return the array. import re input_string = "Hello I am going to I with Hello am" print(len(re.findall('hello', input_string.lower())))

  7. for loop in python with counter? - Stack Overflow

    Jun 10, 2018 · Note: if you're using python2, use xrange is preferred. This just gets you the index, not the index and value. It's fast and memory efficient. range (or xrange) is lazy and you're using an index directly into the array, so you don't have to touch each element. Itertools. If you would like a lazy sequence, you could use itertools. This might be ...

  8. How to add or increment single item of the Python Counter class

    Use case: Counter is very nice because of its defaultdict-like behavior of providing a default zero for missing keys when checking later: >>> c = Counter() >>> c['i'] 0 I find myself doing this a lot as I'm working out the logic for various has_some_property checks (especially in a notebook).

  9. python - Get loop count inside a for-loop - Stack Overflow

    Built in iteration counter for python. 0. ... Adding title to HTML table using python-1.

  10. Using a counter in a python function - Stack Overflow

    Sep 19, 2022 · The other answer is correct, in that the issue is that the counter is reset at the beginning of each function call. The simplest alternative, which I do not recommend, is to use a global counter variable. The other answer (there's only one at the moment) passes the iteration along in the recursive call, which also works.

Refresh