Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,086 questions

55,959 answers

573 users

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
...