List manipulation with dictionary inside a Python loop

Asked

Viewed 1,599 times

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??

1 answer

1


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

But you have another problem: you’re creating a lot of interconnected data structures by hard-to-see keys. What you really want is to use your data efficiently.

Okay, cool, your listNew will contain keys to the word dictionary, it will be more compact, maybe allow you to compare where equal words occur - but what do you really want to do at the end? This list would not be as efficient, and would be even more confusing to use than its original list.

Isn’t it better to put everything in an SQL database? Or in Mongodb?

Ok, while you do not rethink your problem, have how to solve by creating the "listNew" with:

listaNova = [list() for i in range(Len(list))]

and your code above. But that may take - because for every word it will do a linear search in your dictionary. The ideal is to invert the dictionary before, hence each word you find the direct index. 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)

  • worked out here! Thank you so much for your help! and because you passed that -1 inside that way . get()?

  • 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.

  • Ahh got it! Thank you so much for your help!

  • if you need it badly, the "extradict" external library has a dictionary that keeps the X key value reverse ratio automatically.

Browser other questions tagged

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