简体   繁体   English

如何检查列表中的数字而不检测具有相同数字的数字?

[英]How to check for number from a list without detecting numbers that have the same digits?

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 说我有一个清单: [2 dogs play, 4 dogs play, 22 dogs play, 24 dogs play, 26 dogs play]我有一个表达式要求用户输入一个数字,它存储在变量中, 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? 我的问题是,如果用户输入2,代码也将对具有22,24和26的项执行某些操作,因为它中有2个,当我只希望它对只有2的项执行某些操作时。我该如何做到这一点?

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') 使用此方法的输出是func_do('2 dogs play')

B) 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') 使用此方法的输出仍然是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().strip()或:

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 . 另外:这个答案显然还依赖于你试图匹配的用户输入字符串,它位于list item字符串的开头。

You need to get the digits from the item and then check if it is equal to num : 您需要从item获取digits ,然后检查它是否equal 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() : 使用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

There will be many ways to solve this problem, one way using regex is : 有很多方法可以解决这个问题,使用regex一种方法是:

import re

for item in a:
   if re.search("^"+num+" ", item):
        print(item) 
        # Cool logic goes here 
 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. 如果你想明确地将它与int进行比较,那么其他提供的答案就是你要找的答案。

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. 代码非常简单,只有当列表中的每个项目都以数字后跟空格开头时才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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