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.
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.
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.
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.
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
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.
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.
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 ...
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})
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}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With