Home > Net >  How to check a list for matches of in input
How to check a list for matches of in input

Time:12-28

I am trying to make a program that can check an input, then it will be able to search through a list for matches in that string (no spaces) List: {'123', '1234', '12345', '123456', password} input: Hello231234

I want it so that it can check my input for any matches in the list. (I am a beginner in python)

CodePudding user response:

First, a list is noted with brackets: []. Whats is noted as {}, can be either a dictionary if it has key:value as {'name: 'Mike', 'age': 78} either a set as {1, 3, 4, 6, 8}. A set is similar to a list but orders your values and doesn't keep duplicates.

set(["A", 2, True, 3, 3, "a", True, 3.4, "z", "A", True, "z"])

>>> {2, 3, 3.4, 'A', True, 'a', 'z'}

Look for the different data structures in python to understand more of what you can do with it!

For your problem, you want to use a list.

input = "Hello231234"
liste = ['123', '1234', '12345', '123456', 'password']

input in liste

>>> False

By doing that, you check if your input is in liste. By saying input in liste it will return a boolean, True if in, or False if not.

CodePudding user response:

I'm interpreting this as you are looking to see if any of the strings in your list occur in the input (basically the reverse of what @Marc assumed).

In that case, you can loop through your list, and check if any of them exist in the input:

input = "Hello231234"
wordList = ['123', '1234', '12345', '123456', 'password']
match = False
for word in wordList:
    if word in input:
        match = True
        
print(match)
 
  • Related