AlyssaAlex
AlyssaAlex

Reputation: 716

How can I replace a word in my list of lists that matches a word from 1st list into a word that has the same position as the 1st one in the 2nd list?

I have a string type list of lists that has words that need to be replaced.

This replacement can occur by checking if there is a match in list 1, if there is then the position of the match is searched in list 2. The word is picked from that position in list 2 and replaced in the original list of lists.

Note: The matches might not occur in all the sublists of my list of lists.


list_of_list = [['apple', 'ball', 'cat'], ['apple', 'table'], ['cat', 'mouse', 'bootle'], ['mobile', 'black'], ['earphone']]

list_1 = ["apple", "bootle", "earphone"]

list_2 = ["fruit", "object", "electronic"]

This is what I have tried so far, but it doesn't seem to work the way I want it to. 

for word in list_of_list:
    for idx, item in enumerate(word):
        for w in list_1:
            if (w in item):
                index_list_1= list_1.index(w)
                word_from_list_2 = list_2[index_list_1]
                word[idx] = word_from_list_2
print(list_of_list)

I want something like:

Input:

list_of_list = [['apple', 'ball', 'cat'], ['apple', 'table'], ['cat', 'mouse', 'bootle'], ['mobile', 'black'], ['earphone']]

Output:

list_of_list = [['fruit', 'ball', 'cat'], ['fruit', 'table'], ['cat', 'mouse', 'object'], ['mobile', 'black'], ['electronic']]

Upvotes: 2

Views: 116

Answers (2)

yatu
yatu

Reputation: 88236

Here's one way using a nested list comprehension, and mapping the values in the nested list with a dictionary build from list_1 and list_2:

d = dict(zip(list_1, list_2))
# {'apple': 'fruit', 'bootle': 'object', 'earphone': 'electronic'}
[[d.get(i, i) for i in l] for l in list_of_list]

Output

[['fruit', 'ball', 'cat'], ['fruit', 'table'], ['cat', 'mouse', 'object'], 
 ['mobile', 'black'], ['electronic']]

Which is equivalent to the following:

res = [[] for _ in range(len(list_of_list))]
for ix, l in enumerate(list_of_list):
    for s in l:
        res[ix].append(d.get(s, s))
# [['fruit', 'ball', 'cat'], ['fruit', 'table'], ['cat', 'mouse', 'object'], 
#  ['mobile', 'black'], ['electronic']]

Some reads that you might find helpful:

Upvotes: 2

Thierry Lathuille
Thierry Lathuille

Reputation: 24232

If you want to replace the words in the original list in place, without creating a new list, you could do like this:

list_of_list = [['apple', 'ball', 'cat'], ['apple', 'table'], ['cat', 'mouse', 'bootle'], ['mobile', 'black'], ['earphone']]

list_1 = ["apple", "bootle", "earphone"]
list_2 = ["fruit", "object", "electronic"]

replacements = dict(zip(list_1, list_2))

for sublist in list_of_list:
    for index, word in enumerate(sublist):
        if word in replacements:
            sublist[index] = replacements[word]

print(list_of_list)
# [['fruit', 'ball', 'cat'], ['fruit', 'table'], ['cat', 'mouse', 'object'], ['mobile', 'black'], ['electronic']]

Upvotes: 0

Related Questions