Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if any of a list of strings is in another string

In python, I'm trying to create a program which checks if 'numbers' are in a string, but I seem to get an error. Here's my code:

numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]

test = input()

print(test)
if numbers in test:
    print("numbers")

Here's my error:

TypeError: 'in <string>' requires string as left operand, not list

I tried changing numbers into numbers = "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" which is basically removed the [] but this didn't work either. Hope I can get an answer; thanks :)

like image 643
jakethefake Avatar asked Mar 26 '17 18:03

jakethefake


1 Answers

Use built-in any() function:

numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
s = input()

test = any(n in s for n in numbers)
print(test)
like image 87
RomanPerekhrest Avatar answered Nov 15 '22 08:11

RomanPerekhrest