Last active
March 4, 2024 19:50
-
-
Save TGITS/8b5dfde8eb57c1b304b4e98ed909b665 to your computer and use it in GitHub Desktop.
Frequencies map with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import groupby | |
from collections import Counter | |
def frequencies_map_with_for_from(elements): | |
frequencies_map = {} | |
for element in elements: | |
if element in frequencies_map.keys(): | |
frequencies_map[element] = frequencies_map[element] + 1 | |
else: | |
frequencies_map[element] = 1 | |
return frequencies_map | |
def frequencies_map_with_map_and_groupby_from(elements): | |
return dict(map(lambda t: (t[0], len(list(t[1]))), groupby(sorted(elements)))) | |
def frequencies_map_with_comprehension_and_groupby_from(elements): | |
return {k: len(list(v)) for k, v in groupby(sorted(elements))} | |
def frequencies_map_with_counter_from(elements): | |
return Counter(elements) | |
def frequencies_map_with_comprehension_and_groupby_from(elements): | |
return {k: len(list(v)) for k, v in groupby(sorted(elements))} | |
def frequencies_map_with_counter_from(elements): | |
return Counter(elements) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You will need to have pytest install (globally or in a virtual env) | |
from frequencies_map import frequencies_map_with_for_from, frequencies_map_with_map_and_groupby_from, frequencies_map_with_comprehension_and_groupby_from, frequencies_map_with_counter_from | |
class TestFrequenciesMapWithFor: | |
def test_empty_list(self): | |
assert frequencies_map_with_for_from([]) == {} | |
def test_single_element_list(self): | |
assert frequencies_map_with_for_from(["a"]) == {"a": 1} | |
def test_several_identical_elements_list(self): | |
assert frequencies_map_with_for_from(["a", "a", "a"]) == {"a": 3} | |
def test_several_different_elements_list_sorted(self): | |
assert frequencies_map_with_for_from(["a", "a", "a", "b", "b", "c", "d", "d", "d", "d"]) == {"a": 3, "b": 2, "c": 1, "d": 4} | |
def test_several_different_elements_list_unsorted(self): | |
assert frequencies_map_with_for_from(["a", "b", "c", "a", "d", "a", "d", "b", "d", "d"]) == {"a": 3, "b": 2, "c": 1, "d": 4} | |
class TestFrequenciesMapWithMapAndGroupBy: | |
def test_empty_list(self): | |
assert frequencies_map_with_map_and_groupby_from([]) == {} | |
def test_single_element_list(self): | |
assert frequencies_map_with_map_and_groupby_from(["a"]) == {"a": 1} | |
def test_several_identical_elements_list(self): | |
assert frequencies_map_with_map_and_groupby_from(["a", "a", "a"]) == {"a": 3} | |
def test_several_different_elements_list_sorted(self): | |
assert frequencies_map_with_map_and_groupby_from(["a", "a", "a", "b", "b", "c", "d", "d", "d", "d"]) == {"a": 3, "b": 2, "c": 1, "d": 4} | |
def test_several_different_elements_list_unsorted(self): | |
assert frequencies_map_with_map_and_groupby_from(["a", "b", "c", "a", "d", "a", "d", "b", "d", "d"]) == {"a": 3, "b": 2, "c": 1, "d": 4} | |
class TestFrequenciesMapWithComprehensionAndGroupBy: | |
def test_empty_list(self): | |
assert frequencies_map_with_comprehension_and_groupby_from([]) == {} | |
def test_single_element_list(self): | |
assert frequencies_map_with_comprehension_and_groupby_from(["a"]) == {"a": 1} | |
def test_several_identical_elements_list(self): | |
assert frequencies_map_with_comprehension_and_groupby_from(["a", "a", "a"]) == {"a": 3} | |
def test_several_different_elements_list_sorted(self): | |
assert frequencies_map_with_comprehension_and_groupby_from(["a", "a", "a", "b", "b", "c", "d", "d", "d", "d"]) == {"a": 3, "b": 2, "c": 1, "d": 4} | |
def test_several_different_elements_list_unsorted(self): | |
assert frequencies_map_with_comprehension_and_groupby_from(["a", "b", "c", "a", "d", "a", "d", "b", "d", "d"]) == {"a": 3, "b": 2, "c": 1, "d": 4} | |
class TestFrequenciesMapWithCounter: | |
def test_empty_list(self): | |
assert frequencies_map_with_counter_from([]) == {} | |
def test_single_element_list(self): | |
assert frequencies_map_with_counter_from(["a"]) == {"a": 1} | |
def test_several_identical_elements_list(self): | |
assert frequencies_map_with_counter_from(["a", "a", "a"]) == {"a": 3} | |
def test_several_different_elements_list_sorted(self): | |
assert frequencies_map_with_counter_from(["a", "a", "a", "b", "b", "c", "d", "d", "d", "d"]) == {"a": 3, "b": 2, "c": 1, "d": 4} | |
def test_several_different_elements_list_unsorted(self): | |
assert frequencies_map_with_counter_from(["a", "b", "c", "a", "d", "a", "d", "b", "d", "d"]) == {"a": 3, "b": 2, "c": 1, "d": 4} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment