How to check if a specific digit is in an integer

前端 未结 5 1045
我在风中等你
我在风中等你 2020-12-11 21:58

I want to check if, for example, the digit \'2\' is in 4059304593. My aim is to then check if there are any of the digits 1-9 not in my integer. This is what I have tried:

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-11 22:17

    You could convert your number to a string, then to a set to get the unique digits.

    You just have to iterate over digits in 0-9 to find the ones not present in your original number :

    >>> set(map(int,str(4059304593)))
    set([0, 9, 3, 4, 5])
    >>> digits = _
    >>> [i for i in range(10) if i not in digits]
    [1, 2, 6, 7, 8]
    

提交回复
热议问题