Quick actions

cmd+k|ctrl+k

Navigation

Languages

Frequency using Counter

Snippet info

Language

Python

Visibility

public

Author

hzx33737

Created

2023-05-16T03:05:39.396425Z

Updated

2023-05-16T03:05:39.396425Z

from collections import Counter

#### FREQUENCY OF STRING ####
msg = "Hello"

freq_msg = Counter(msg)
print(freq_msg) 
# Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1})

print("Frequencies are: ")
for key, value in freq_msg.items():
    print(key, value)
    
# Frequencies are: 
# H 1
# e 1
# l 2
# o 1

#### FREQUENCY OF LIST ####
my_list = [11, 5, 10, 11, 11, 5, 10, 5, 5]

freq_my_list = Counter(my_list)
print(freq_my_list)
# Counter({5: 4, 11: 3, 10: 2})

print("Frequencies are: ")
for key, value in freq_my_list.items():
    print(key, value)
    
# Frequencies are: 
# 11 3
# 5 4
# 10 2
INFO