Skip to content

Instantly share code, notes, and snippets.

@TGITS
Last active Oct 23, 2020
Embed
What would you like to do?
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_from(elements):
return dict(map(lambda t: (t[0], len(list(t[1]))), groupby(sorted(elements))))
def frequencies_map_with_comprehension_from(elements):
return {k: len(list(v)) for k, v in groupby(sorted(elements))}
def frequencies_map_with_counter_from(elements):
return Counter(elements)
import unittest
from frequencies_map import frequencies_map_with_comprehension_from, frequencies_map_with_for_from, frequencies_map_with_map_from, frequencies_map_with_counter_from
class FrequenciesMapWithForTest(unittest.TestCase):
def test_empty_list(self):
self.assertEqual(frequencies_map_with_for_from([]), {})
def test_single_element_list(self):
self.assertEqual(frequencies_map_with_for_from(["a"]), {"a": 1})
def test_several_identical_elements_list(self):
self.assertEqual(frequencies_map_with_for_from(
["a", "a", "a"]), {"a": 3})
def test_several_different_elements_list_sorted(self):
self.assertEqual(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):
self.assertEqual(frequencies_map_with_for_from(
["a", "b", "c", "a", "d", "a", "d", "b", "d", "d"]), {"a": 3, "b": 2, "c": 1, "d": 4})
class FrequenciesMapWithMapTest(unittest.TestCase):
def test_empty_list(self):
self.assertEqual(frequencies_map_with_map_from([]), {})
def test_single_element_list(self):
self.assertEqual(frequencies_map_with_map_from(["a"]), {"a": 1})
def test_several_identical_elements_list(self):
self.assertEqual(frequencies_map_with_map_from(
["a", "a", "a"]), {"a": 3})
def test_several_different_elements_list_sorted(self):
self.assertEqual(frequencies_map_with_map_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):
self.assertEqual(frequencies_map_with_map_from(
["a", "b", "c", "a", "d", "a", "d", "b", "d", "d"]), {"a": 3, "b": 2, "c": 1, "d": 4})
class FrequenciesMapWithComprehensionTest(unittest.TestCase):
def test_empty_list(self):
self.assertEqual(frequencies_map_with_comprehension_from([]), {})
def test_single_element_list(self):
self.assertEqual(
frequencies_map_with_comprehension_from(["a"]), {"a": 1})
def test_several_identical_elements_list(self):
self.assertEqual(frequencies_map_with_comprehension_from(
["a", "a", "a"]), {"a": 3})
def test_several_different_elements_list_sorted(self):
self.assertEqual(frequencies_map_with_comprehension_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):
self.assertEqual(frequencies_map_with_comprehension_from(
["a", "b", "c", "a", "d", "a", "d", "b", "d", "d"]), {"a": 3, "b": 2, "c": 1, "d": 4})
class FrequenciesMapWithCounterTest(unittest.TestCase):
def test_empty_list(self):
self.assertEqual(frequencies_map_with_counter_from([]), {})
def test_single_element_list(self):
self.assertEqual(
frequencies_map_with_counter_from(["a"]), {"a": 1})
def test_several_identical_elements_list(self):
self.assertEqual(frequencies_map_with_counter_from(
["a", "a", "a"]), {"a": 3})
def test_several_different_elements_list_sorted(self):
self.assertEqual(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):
self.assertEqual(frequencies_map_with_counter_from(
["a", "b", "c", "a", "d", "a", "d", "b", "d", "d"]), {"a": 3, "b": 2, "c": 1, "d": 4})
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment