Mac R62 Mac R62 - 2 years ago 60
Python Question

Utilizing any word within a string to locate an item within a list and identifying item

Problem: utilizing any word within a string to locate an item within a list and identifying item or return an index.

Hello, I have been searching this site and other sites to find a solution for my problem. The solutions that I have found are successful in determining if the list has a item in the string,


any(word in str1 for word in List)

But this only returns true or false, and with the command,

print word

returns an error

I need a solution that can locate the list item and either print the item, or give the index within the list.

The project I am using this for as an AI that has an ability to preform math equation. For this it needs to be able to locate operators that are not only the standard " +,-,*,/ " but also the word formats. For example, " plus, minus, etc." When located, the program can simply replace items in the string with proper standard operators that eval() can handle.

Sample code reapplied from project:

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

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):

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

Any help or advice on this code would be greatly appreciated.

Answer Source

I'm going to ignore the part about your AI project, as it isn't a MCVE. I think the first part of your question is, though, so I'll show you how to find the item and index where items in a list occur that satisfy the condition you give (word in str1) using numpy.where:

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

This snippet will output:

[False, True, False, True, False]
[1, 3]

A word of advice: Don't use a capital letter for a variable like List. I kept my answer like this because I wanted you to be able to easily follow my solution. Typically capital letters denote a class name, so capitalizing variables makes your code harder for other Python programmers to read and comprehend. This is just a stylistic norm in the Python community.

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download