Reputation: 25
Say I have a list: [2 dogs play, 4 dogs play, 22 dogs play, 24 dogs play, 26 dogs play]
I have an expression that asks a user for a number and it's stored in the variable, num
I have a condition in my code where,
for item in list:
if num in item:
....
do something to item
My problem is that if the user inputs 2, the code will also do something to the items that have 22, 24, and 26 because it has 2 in it, when I only want it to do something to the item that has only 2. How do I accomplish this?
Upvotes: 1
Views: 108
Reputation: 1857
There will be many ways to solve this problem, one way using regex
is :
import re
for item in a:
if re.search("^"+num+" ", item):
print(item)
# Cool logic goes here
Upvotes: 1
Reputation: 136
Solution:
Change your function to:
for item in list:
# Since it's obvious you're checking a string for a number (in string form), you need
# to make sure a space comes after the number (at the beginning of the string) in order
# to avoid incomplete matches.
if item.startswith(num, beg=0, end=len(num)):
...
func_do(item)
Results:
A)
['2 dogs play', '4 dogs play', '22 dogs play', '24 dogs play', '26 dogs play']
num = '2' # input with no space trailing
Output using this method is the result of func_do('2 dogs play')
B)
['2 dogs play', '4 dogs play', '22 dogs play', '24 dogs play', '26 dogs play']
num = '2 ' # input with space trailing (user hit spacebar and we didn't `trim()` the input)
Output using this method is still the result of func_do('2 dogs play')
Beware:
Sanitize your input, if you use any other method provided so far (or any method that checks for spaces following the input) you will have to be wary of a user entering a number with a space after it (or before it).
Use num = input().strip()
or:
num = input()
num = num.strip()
ALSO: This answer also obviously relies on the user-input string that you're trying to match residing at the beginning of the item
strings from your list
.
Upvotes: 1
Reputation: 295
I took the super simple approach of splitting each item in the list and querying the first.
num = 2
list = ['2 dogs play', '4 dogs play', '22 dogs play', '24 dogs play', '26 dogs play']
for item in list:
number_of_dogs = int(item.split(' ')[0])
if number_of_dogs == num:
print('Doing something!')
The code is very simplistic and only works if each item in the list starts with the number followed by a space.
Upvotes: 0
Reputation: 16772
You need to get the digits
from the item
and then check if it is equal
to num
:
Using regex:
import re
uList = ['2 dogs play', '4 dogs play', '22 dogs play', '24 dogs play', '26 dogs play']
num = int(input("Enter a num: "))
for item in uList:
if num == int((re.findall(r'\d+', item))[0]):
print(item)
OUTPUT:
Enter a num: 2
2 dogs play
Process finished with exit code 0
EDIT:
Using split()
:
uList = ['2 dogs play', '4 dogs play', '22 dogs play', '24 dogs play', '26 dogs play']
num = input("Enter a num: ") # no conversion to int here
for item in uList:
if num == item.split(' ')[0]: # split and get the digits before space
print(item)
OUTPUT:
Enter a num: 22
22 dogs play
Process finished with exit code 0
Upvotes: 1
Reputation: 384
num= raw_input("enter the number that you want to check")
if(num==item.split(' ', 1)[0])
...do something
that will check if the number exists in the list given that the number comes first in the list
If you want to explicitly compare it to an int then the other provided answers are what you are looking for.
Upvotes: 0