Last active
November 15, 2021 03:04
-
-
Save JosephTLyons/7bc8a94b7e5e2d3115043fcb2e11dbcc to your computer and use it in GitHub Desktop.
A function that uses a Python Counter to print a visual summary of the occurrences of integers in a list
This file contains hidden or 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 collections import Counter | |
from random import randint | |
def visualize(numbers): | |
counter = Counter(numbers) | |
max_number_length = len(str(max(numbers))) | |
for number, count in counter.most_common(): | |
padded_number = str(number).rjust(max_number_length, "0") | |
count_visualization = "*" * count | |
print(f"{padded_number}: {count_visualization} ({count})") | |
numbers = [randint(1, 25) for _ in range(1000)] | |
visualize(numbers) | |
# 12: ***************************************************** (53) | |
# 23: ************************************************ (48) | |
# 22: ********************************************** (46) | |
# 03: ********************************************* (45) | |
# 17: ******************************************** (44) | |
# 14: ******************************************* (43) | |
# 16: ******************************************* (43) | |
# 21: ******************************************* (43) | |
# 05: ****************************************** (42) | |
# 07: ***************************************** (41) | |
# 06: ***************************************** (41) | |
# 25: **************************************** (40) | |
# 18: *************************************** (39) | |
# 20: *************************************** (39) | |
# 19: *************************************** (39) | |
# 13: ************************************** (38) | |
# 01: ************************************** (38) | |
# 09: ************************************** (38) | |
# 15: ************************************* (37) | |
# 08: ************************************* (37) | |
# 24: ********************************** (34) | |
# 04: ********************************** (34) | |
# 02: ********************************* (33) | |
# 11: ********************************* (33) | |
# 10: ******************************** (32) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment