import collections
a_list = ["a", "b", "a"]
occurrences = collections.Counter(a_list)
print(occurrences)
student_grades = [9.1, 8.8, 10.0, 7.7, 6.8, 8.0, 10.0, 8.1, 10.0, 9.9]
samebnumber = student_grades.count(10.0)
print(samebnumber)
from collections import Counter
1ist1 = ['Peter', 'Rose', 'Donald', 'Peter']
a = Counter(listl).get('Peter')
print(f'Peter appears in the list {a} times')
# Output:
# Peter appears in the list 2 times
indices = [index for index, element in enumerate(a_list) if element == 1]
"""Find all occurrences of element in list"""
# If you simply want find how many times an element appears
# use the built-in function count()
def find_occur(lst, item):
return lst.count(item)
# Test -----------------------------------------------------------
print(find_occur([None, None, 1, 2, 3, 4, 5], None)) # 2
# If you wanna find where they occur instead
# - This returns the indices where element is found within a list
def find(lst, item):
return [i for (i, x) in enumerate(lst) if x == item]
# Test Code ------------------------------------------------------
from random import randint, choice
lst = [randint(0, 99) for x in range(10)] # random lst
item = choice(lst) # item to find
found = find(lst, item) # lst of where item is found at
print(f"lst: {lst}",
f"item: {item}",
f"found: {found}",
sep = "
")
Map<String, Long> counts =
list.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));