Python 利用字符串中的任何单词在列表中定位项目并标识项目

Python 利用字符串中的任何单词在列表中定位项目并标识项目,python,string,list,Python,String,List,问题:使用字符串中的任何单词来定位列表中的项,并标识项或返回索引 您好,我一直在搜索这个网站和其他网站,以找到解决我的问题的方法。我找到的解决方案成功地确定了列表中是否有项 例如: any(word in str1 for word in List) 但这只会返回true或false,并且使用命令 print word 返回一个错误 我需要一个解决方案,可以找到列表项,要么打印该项,要么给出列表中的索引 这个项目我用它作为一个人工智能,有能力预成型的数学方程。为此,它需要能够定位不仅是标准“

问题:使用字符串中的任何单词来定位列表中的项,并标识项或返回索引

您好,我一直在搜索这个网站和其他网站,以找到解决我的问题的方法。我找到的解决方案成功地确定了列表中是否有项

例如:

any(word in str1 for word in List)
但这只会返回true或false,并且使用命令

print word
返回一个错误

我需要一个解决方案,可以找到列表项,要么打印该项,要么给出列表中的索引

这个项目我用它作为一个人工智能,有能力预成型的数学方程。为此,它需要能够定位不仅是标准“+,-,*,/”而且是单词格式的运算符。例如,“加号、减号等”。找到后,程序可以简单地用eval()可以处理的适当标准运算符替换字符串中的项

从项目重新应用的示例代码:

from __future__ import division
import re
from math import *
Listopp = ["+","-", "*","/"]
Listadd = ["add","plus"]
Listsub = ["subtract","minus"]
Listmult = ["times","multiply","x"]
Listdivide = [ "divide","over"]
Listmath =Listopp + Listadd + Listsub + Listmult + Listdivide

try:
    str1 = raw_input("what is your math problem?")

    if (any(word in str1 for word in Listmath) and re.findall(r"[-+]?\d*\.\d+|\d+",str1) != []):


# here is where the solution need to be placed

        opp = Listmath.index(any(word in str1))

         # needs to be identified or indexed 


#Replaced with standard operators 
        if (opp in Listdivide):
            str1 = str1.replace( opp ,"/")

        if (opp in Listmult):
            str1 = str1.replace( opp ,"*")

        if (opp in Listsub):
            str1 = str1.replace( opp ,"-")

        if (opp in Listadd):
            str1 = str1.replace( opp ,"+")

        if (opp in Listopp):
            pass

        math = eval(str1)
        if not float(math).is_integer():
            print "rounded"
            result = round(math, 3)
        else:
            print "real"
            result = math
        print result
    else:
        print "No suitable math problems found."
except Exception as e:
    print e

任何关于这段代码的帮助或建议都将不胜感激。

我将忽略关于您的AI项目的部分,因为这不是一个简单的问题。不过,我认为您问题的第一部分是,因此我将向您展示如何使用
numpy查找列表中满足您给定条件的项目和索引(
word in str1
)。其中

import numpy as np
List = ["add", "bolt", "dead", "bolter", "test"]
str1 = "bolter"
items = [word in str1 for word in List]
print items
indices = np.where(items)[0]
print indices
此代码段将输出:

[False, True, False, True, False]
[1, 3]
建议:不要用大写字母表示变量,如
List
。我一直这样回答,因为我希望你能够轻松地遵循我的解决方案。通常大写字母表示类名,因此大写变量会使其他Python程序员更难阅读和理解代码。这只是Python社区的一种风格规范

希望这有帮助

您可以使用。第一个参数是迭代器,第二个(可选)参数是默认值(如果迭代器已耗尽)。下面是一个Python会话,展示了
next()
的不同用法:

>>我的列表=[4,6,1,4,1]
>>>任意(对于我的_列表中的x,x>4)
真的
>>>下一步(如果x>4,则我的_列表中的x对应x)
6.
>>>下一步(i表示i,如果x>4,则枚举(我的_列表)中的x)
1.
>>>下一步(i表示i,如果x>6,则枚举(我的_列表)中的x)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
停止迭代
>>>默认值=4
>>>下一步((i表示i,如果x>6,则枚举(我的_列表)中的x),默认值)
4.
>>>下一步((i表示i,如果x>4,则枚举(我的_列表)中的x),默认值)
1.
我敢肯定你“做错了”。您所描述的是一个表达式解析器,它比搜索和替换要复杂得多。例如,使用pyparsing:
>>> my_list = [4, 6, 1, 4, 1]
>>> any(x > 4 for x in my_list)
True
>>> next(x for x in my_list if x > 4)
6
>>> next(i for i, x in enumerate(my_list) if x > 4)
1
>>> next(i for i, x in enumerate(my_list) if x > 6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> default = 4
>>> next((i for i, x in enumerate(my_list) if x > 6), default)
4
>>> next((i for i, x in enumerate(my_list) if x > 4), default)
1