Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a digit is present in a list of numbers

How can I see if an index contains certain numbers?

numbers = [2349523234, 12345123, 12346671, 13246457, 134123431]

for number in numbers:
    if (4 in number):
        print(number + "True")
    else:
        print("False")
like image 617
Nexxt Avatar asked Sep 24 '15 19:09

Nexxt


People also ask

How do you check if a digit is present in a number in Python?

Python isdigit() method returns True if all the characters in the string are digits. It returns False if no character is digit in the string.

How do you know if a number contains a digit?

bool containsDigit(int number, int digit); If number contains digit, then the function should return true . Otherwise, the function should return false .

How do you check if a certain number is in a list in Python?

We can use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.

How do you check if all elements are in a list of numbers?

Use the all() function to check if all elements in a list are integers, e.g. if all(isinstance(item, int) for item in my_list): . The all() function will return True if all of the elements in the list are integers and False otherwise.


1 Answers

You would have to do string comparisons for this

for number in numbers:
    if '4' in str(number):
        print('{} True'.format(number))
    else:
        print("False")

It isn't really meaningful to ask if the number 4 is "in" another number (unless you have some particular definition of "in" in mind)

like image 104
Cory Kramer Avatar answered Oct 01 '22 14:10

Cory Kramer