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

    4 days ago · from collections import Counter from collections import defaultdict import numpy import operator import pandas import perfplot def counter(a): return Counter(a) def count(a): return dict((i, a.count(i)) for i in set(a)) def bincount(a): return numpy.bincount(a) def pandas_value_counts(a): return pandas.Series(a).value_counts() def occur_dict(a ...

  2. How do I get the length of a list? - Stack Overflow

    Oct 13, 2022 · item_count = 0 for item in list: item_count += 1 return item_count count([1,2,3,4,5]) (The list object must be iterable, implied by the for..in stanza.) The lesson here for new programmers is: You can’t get the number of items in a list without counting them at some point.

  3. 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.

  4. Python - Count elements in list - Stack Overflow

    Nov 9, 2010 · Unfortunately, it seems this is the first google result for python list check number of elements, instead of the linked question that this duplicates. – Drise Commented Dec 14, 2017 at 17:03

  5. python - How do I count occurrence of unique values inside a list ...

    Sep 5, 2012 · So I'm trying to make this program that will ask the user for input and store the values in an array / list. Then when a blank line is entered it will tell the user how many of those values are uni...

  6. count in python list comprehension - Stack Overflow

    Mar 2, 2016 · If info is enumerable and enumerating has the same effect as accessing with a zero offset index (like you seem to do with count, you can rewrite your loop like: lst = [] for infoi in info: num = infoi["count"] # print num lst.append(num) print sum(lst)

  7. Fastest way to count number of occurrences in a Python list

    Sep 17, 2012 · I must be missing something. Working with list[long] datasets (containing random.randint(0, sys.maxsize) numbers, up to 50M of them)` , trying to count another randint with the same parameters, .cont is ~10 times faster than Counter (only try to count once).

  8. Python: how to get sorted count of items in a list?

    Feb 18, 2010 · So this function uses a defaultdict to count the number of each entry in our list. We then take each pair of the entry and its count and sort it in descending order according to the count. We then take the top number of entries and return that. So now we can say

  9. Counting the number of True Booleans in a Python List

    Oct 7, 2012 · I have a list of Booleans: [True, True, False, False, False, True] and I am looking for a way to count the number of True in the list (so in the example above, I want the return to be 3.) I have ...

  10. Python: count number of elements in list for if condition

    Mar 12, 2016 · The generator expression is more memory efficient, because you don't have to create an extra list.. Creating a list and getting it's length (the latter being a very fast O(1) operation) seems to be faster than creating a generator and doing n …

Refresh