xxxxxxxxxx
list1=list(input("Enter list:"))
length_list1=len(list1)
uniq=[]
dupl=[]
count = i = 0
while i < length_list1:
element=list1[i]
count=1
if element not in uniq and element not in dupl:
i += 1
for j in range(i, length_list1):
if element == list1[j]:
count += 1
else:
print("Element", element, "frequency", count)
if count == 1:
uniq.append(element)
else:
dupl.append(element)
else:
i += 1
xxxxxxxxxx
# easiest way to count the frequency of all elements in a list
lst = ['Sam', 'Sam', 'Tim', 'Tim', 'Tim', 'r', 'l']
freq = {} # stores the frequency of elements
counting = [freq.update({x: lst.count(x)}) for x in lst]
# output of freq
{'Sam': 2, 'Tim': 3, 'r': 1, 'l': 1}
#credit: buggyprogrammer.com
#Note: if you print "counting" it will return a list full of None so ignore it.
xxxxxxxxxx
from collections import Counter
def frequency_table(n):
table = Counter(n)
print('Number\tFrequency')
for number in table.most_common() :
print('{0}\t{1}'.format(number[0], number[1]))
# src : Doing Math With Python
xxxxxxxxxx
def count_frequency(numbers):
frequency = {}
for num in numbers:
if num in frequency:
frequency[num] += 1
else:
frequency[num] = 1
return frequency
numbers = [1, 2, 2, 3, 1, 4, 2, 4, 5]
frequency = count_frequency(numbers)
print("Frequency:", frequency)