How to find the frequency of each digit (0–9) in a number with Python

4 Answers

0 votes
from collections import Counter
 
num = 52725510215
freq = Counter(str(num))
 
print(freq)

 
'''
run:
 
Counter({'5': 4, '2': 3, '1': 2, '7': 1, '0': 1})
 
'''

 



answered Jul 2 by avibootz
0 votes
from collections import Counter

num = 52725510215
freq = Counter(str(num))

for digit in '0123456789':
    print(digit, freq.get(digit, 0))



'''
run:

0 1
1 2
2 3
3 0
4 0
5 4
6 0
7 1
8 0
9 0

'''

 



answered Jul 2 by avibootz
0 votes
num = 52725510215
freq = {str(d): 0 for d in range(10)}

for ch in str(num):
    freq[ch] += 1

print(freq)



'''
run:

{'0': 1, '1': 2, '2': 3, '3': 0, '4': 0, '5': 4, '6': 0, '7': 1, '8': 0, '9': 0}

'''

 



answered Jul 2 by avibootz
0 votes
num = 52725510215
freq = [0] * 10

for ch in str(num):
    freq[int(ch)] += 1

print(freq)




'''
run:

[1, 2, 3, 0, 0, 4, 0, 1, 0, 0]

'''

 



answered Jul 2 by avibootz

Related questions

...