简体   繁体   中英

Python data analysis serial number

Hi I have a serial number, for example 204567 and each number of the 6 numbers has subset data. Is there a way I can cipher through each digit in python?

from math import floor

def check_if_number_exists(number, digit_to_find):
    # check if the number is positive
    if number < 0:
        Number = -number
    else:
        Number = number
    while(Number != 0):
        # run loop for extracting each digit
        Digit = Number % 10 # gives the last number
        Number = floor(Number / 10) # removing the last digit
        if Digit == digit_to_find:
            print("the number contains " + str(digit_to_find) + ' in ' + str(number))
            break


#                      sequencenumber, number_to_check                    
check_if_number_exists(45638, 3)

There is another way as well, you can convert number to string and then check each element of the string !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM