Problem identifying if a word from a list is present in my string in Python

Asked

Viewed 54 times

-1

I’m looking to identify if in my input there are any words that are on a list of words that I created. The code runs normal without presenting any error, however, is missing the result always.

Code:

x = input('Insira aqui o email:\n')
x = re.sub('-'," ", x)
x = str(x)
x=x.lower()
x=x.translate(punc_table)
import csv
with open("fornecedores.csv") as f_obj:
  reader = csv.reader(f_obj)
  providerlist = list(reader)

providerlist = [[k.lower()] for l in providerlist for k in l]
x = x.split()
res = [ele for ele in x if(ele in providerlist)]
print("Does string contain any list element : " + str(bool(res))) 
  • What is the result that is coming out?

  • Always returns False. Even though my input has a single word that is present in.csv vendors

1 answer

0

The problem is I was creating a list of lists. For example: [['supplier'1'],['supplier'2']] The solution was as follows:

c = len (providerlist)

for i in range(c):
  providerlist += providerlist[i]

Now the list is: ['supplier','supplier'2'] Solving the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.