So, for a moment let's imagine that we have two lists:

AI = [{"Паша Техник": 1}, {"Паша Техник жив": 2}]

in1 = ["Паша", "Техник"]

using the for loop, you need to try to find each item from the list in1 in the list of AI

I tried to do it through the index () method, and already found the element index and work with it, but Python politely told me "No, boy, that won't work! Either you work with me normally, or I raise this error edge!"

For example, you need to find the word "Pasha" in AI

In the end, I did not work out, maybe you have an idea?

 import re ai = [{"Паша Техник": "1"}, {"Паша Техник жив": "2"}] in1 = [] #print(ai.index("как ты?")) while True: key = input(" >> ") # разделение сообщения на части списка key = key.split(" ") for z in key: # Работа с индексом, тут и была ошибка in1.append(ai.index(z)) print(int1) 

Mistake:

 Traceback (most recent call last): File "ai.py", line 15, in <module> in1.append(ai.index(z)) ValueError: 'Паша' is not in list 
  • 2
    Show your code and full error text. - Sergey Gornostaev
  • import re ai = [{"Паша Техник": "1"}, {"Паша Техник жив": "2"}] in1 = [] #print(ai.index("как ты?")) while True: key = input(" >> ") # разделение сообщения на части списка key = key.split(" ") for z in key: # Работа с индексом, тут и была ошибка in1.append(ai.index(z)) print(int1) - Ar4ikov
  • root @ Ar4ikov: / AI # python3.5 ai.py >> Pasha Technician Traceback (most recent call last): File "ai.py", line 15, in <module> in1.append (ai.index (z)) ValueError: 'Pasha' is not in list root @ Ar4ikov: / AI # - Ar4ikov
  • Attach sample input / output data - Pavel Durmanov
  • Input >> Pasha Technician Output >> 1 Input >> Pasha Technician Alive Output >> 2 - Ar4ikov

1 answer 1

 In [49]: def foo(word): ...: for i in AI: ...: if word in i: ...: return i[word] ...: In [50]: foo('Паша Техник') Out[50]: 1 In [51]: foo('Паша Техник жив') Out[51]: 2