Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the multiple occurrences in a set

Tags:

python

if group not in g:
    g[group] = set()
g[group].add(name)

goes through a list of groups with this structure:

Group: A
Name: Bob

and adds to the set the names of persons belonging to a specific group. The names in the set are unique and we don't know how many similar names there are in a group. So e.g. if there are two 'Bob' names or 5 'Mike' names, how can I count the multiple occurrences of the names as well to have something like this:

Group A: Bob 2, Mike 5
Group B: Jane 4

and so on. Thanks in advance.

like image 363
Adia Avatar asked Jan 20 '11 11:01

Adia


People also ask

How do you count occurrences of elements in a list?

Using the count() Function The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.

How do you count multiple occurrences in Python?

The easiest way to count the number of occurrences in a Python list of a given item is to use the Python . count() method. The method is applied to a given list and takes a single argument. The argument passed into the method is counted and the number of occurrences of that item in the list is returned.

How do you count the number of specific elements in a list in Python?

The count() is a built-in function in Python. It will return you the count of a given element in a list or a string. In the case of a list, the element to be counted needs to be given to the count() function, and it will return the count of the element. The count() method returns an integer value.

How to count the number of occurrences of each value?

Using the COUNTIF function we can count the number of occurrences of each value in a column or range. The COUNTIF function counts the number of cells within a range comparing a particular condition. Syntax or generic formula of COUNTIF is as follows range: The range of cells you want to count

How do you count occurrences with multiple criteria in Excel?

Count number of occurrences with multiple criteria. EXCEL. = COUNTIFS (B9:B15,C5,C9:C15,">"&C6) This formula counts the number of occurrences where range (B9:B15) captures the word "Bread" and in the corresponding cell in range (C9:C15) it captures a value greater than 400.

How do I count how many times a particular value appears?

Use the COUNTIF function to count how many times a particular value appears in a range of cells. For more information, see COUNTIF function. The COUNTIFS function is similar to the COUNTIF function with one important exception: COUNTIFS lets you apply criteria to cells across multiple ranges and counts the number of times all criteria are met.

How to count number of occurrences (or frequency) in a sorted array?

Count number of occurrences (or frequency) in a sorted array 1 Use Binary search to get index of the first occurrence of x in arr []. Let the index of the first occurrence be i. 2 Use Binary search to get index of the last occurrence of x in arr []. Let the index of the last occurrence be j. 3 Return (j – i + 1); More ...


2 Answers

Looks like you might be better off with a Counter:

>>> from collections import Counter
>>> mylist = ["Bob", "Mike", "Bob", "Mike", "Mike", "Mike", "Bob"]
>>> Counter(mylist)
Counter({'Mike': 4, 'Bob': 3})
like image 134
Tim Pietzcker Avatar answered Oct 07 '22 01:10

Tim Pietzcker


use a dict of dicts to count, e.g. as follows:

tralala = dict()

for group, name in [('A', 'Bob'), ('B', 'Jane'), ('A', 'Bob')]:
    tralala.setdefault(group, dict()).setdefault(name, 0) 
    tralala[group][name] += 1

print tralala

This results in

{'A': {'Bob': 2}, 'B': {'Jane': 1}}
like image 36
Uwe Kleine-König Avatar answered Oct 06 '22 23:10

Uwe Kleine-König