1
I have a list of lists, in which the internal lists contain words and each internal list represents the words of each different file.
lista = [['carro','barco'],['moto','aviao'],['bike','skate']]
ie the position lista[0]
represents the word set of the file a.txt
I also have a dictionary structure that contains words, the key of the dictionary is serving to enumerate each word. In this way:
dic = {0:'bike',1:'carro',2:'caminhao',3:'navio',4:'jato',5:'moto'}
My intention is to save in another listaNova = [[]]
, and each set of words of lista[i]
has a word that is equal to some dictionary key value I keep the dictionary key in this new list, keeping the idea of what each dictionary position listaNova[i]
is representing a file.
My problem is how to make this loop to compare the values.. I’ve tried several ways but none worked.... I’m doing something like:
for i in range(len(lista)):
for item in lista[i]:
for key, value in dic.items():
if value == item:
listaNova[i].append(key)
would be more or less the way??
worked out here! Thank you so much for your help! and because you passed that -1 inside that way . get()?
– William Henrique
The second optional parameter for get is the value that will be returned if the key does not exist in the dictionary. So if any word does not exist, it will appear with the index "-1" in the new list.
– jsbueno
Ahh got it! Thank you so much for your help!
– William Henrique
if you need it badly, the "extradict" external library has a dictionary that keeps the X key value reverse ratio automatically.
– jsbueno