List manipulation with dictionary within a loop in Python

0

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 still have a dictionary structure that contains words, the dictionary key is serving to enumerate each word. 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 Value of the dictionary key I keep the dictionary key in this new list, keeping the idea of that each position of listaNova[i] is representing a file. To my problem is how to make this loop to compare the values .. already 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 it be more or less the way?

    
asked by anonymous 09.11.2017 / 14:59

1 answer

1

Your algorithm would work. The problem here would be to create your "newlist" previously, with a list for each position in the original list (and hit == on if).

But you have another problem: you are making a lot of data structures interconnected by hard-to-see keys. Quanod you actually want is to use your data efficiently.

Ok, cool, your new list will contain keys to the dictionary of words, it will be more compact, perhaps allow you to compare where identical words occur - but what do you really want to do in the end? This new list would not be as efficient, and it would be even more confusing to use than your original list.

Is not it better to put everything in a SQL database? Or in MongoDB?

Okay, as long as you do not rethink your problem, you can solve it by creating the "NewList" with:

newlist = [list () for i in range (len (list))]

and your code above. But this can take a long time - because for each word it will do a linear search in your dictionary. The ideal is to invert the dictionary before, hence of each word you find the index direct. With this, you can solve with a comprehension:

inverted_dic = {value:key for key, value in dic.items()}
lista_nova =  [[inverted_dic.get(palavra, -1) for palavra in item] for item in lista]

(O .get avoids an exception if the word does not exist in your dictionary)

    
09.11.2017 / 15:35