Introduction
In Python, you can group elements of a list based on a given function. This can be useful when you want to count the number of occurrences of each group. In this challenge, you will be tasked with creating a function that groups the elements of a list based on a given function and returns the count of elements in each group.
Count Grouped Elements
Write a function count_by(lst, fn = lambda x: x) that takes a list lst and a function fn as arguments. The function should group the elements of the list based on the given function and return a dictionary with the count of elements in each group.
To solve this problem, you can follow these steps:
- Initialize a dictionary using
collections.defaultdict. - Use
map()to apply the given function to each element of the list. - Iterate over the mapped values and increase the count of each element in the dictionary.
The function should return the resulting dictionary.
from collections import defaultdict
def count_by(lst, fn = lambda x: x):
count = defaultdict(int)
for val in map(fn, lst):
count[val] += 1
return dict(count)
from math import floor
count_by([6.1, 4.2, 6.3], floor) ## {6: 2, 4: 1}
count_by(['one', 'two', 'three'], len) ## {3: 2, 5: 1}
Summary
In this challenge, you learned how to group elements of a list based on a given function and return the count of elements in each group. You also learned how to use collections.defaultdict and map() to solve this problem.