Skip to content

Instantly share code, notes, and snippets.

@TGITS
Last active March 4, 2024 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TGITS/8b5dfde8eb57c1b304b4e98ed909b665 to your computer and use it in GitHub Desktop.
Save TGITS/8b5dfde8eb57c1b304b4e98ed909b665 to your computer and use it in GitHub Desktop.
Frequencies map with Python
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)
# 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