-6

How can I count how many different numbers there are in one long number?

For example: this number 1122334455 has 5 different numbers.

How can I do that in Python?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Skizo
  • 521
  • 2
  • 8
  • 14
  • 2
    possible duplicate of [Count unique digits one liner (efficiently)](http://stackoverflow.com/questions/10752434/count-unique-digits-one-liner-efficiently) – Ashwini Chaudhary May 02 '14 at 10:59
  • You should change the term `different numbers` to `different decimal digits` (assuming that's what you actually want to count). – barak manos May 02 '14 at 11:05

1 Answers1

4

You can do that as:

print len(set(str(s)))

The str() casts the int as a string
The set() takes the unique elements of the string and creates a set of it
The len() returns the length of the set

Examples

>>> print len(set(str(s)))
5

s = 1324082304

>>> print len(set(str(s)))
6
Community
  • 1
  • 1
sshashank124
  • 31,495
  • 9
  • 67
  • 76