tumbleweed
tumbleweed

Reputation: 4640

Problems while trying to replace a list of tuples?

I have the following lists of tuples:

list1 = [('the', 'a'), ('over','1'), ('quick','b'),('fox', 'c'), ('brown', 'd'), ('dog','e'), ('jumps','2')]

I am trying to replace some tuples as follows (*):

('the', 'a') -> ('the', 'ART')
('quick','b') -> ('quick','VERB')
('fox', 'c') -> ('fox', 'ANIMAL')

In other words, the tuples of list1, should be replaced like this:

list1 = [('the', 'ART'), ('over','1'), ('quick','VERB'),('fox', 'ANIMAL'), ('brown', 'd'), ('dog','e'), ('jumps','2')]

So, I tried with the following function:

def replace_val(alist, key, value):
    return [(k,v) if (k != key) else (key, value) for (k, v) in alist]

The problem is that I can't pass all the new tuples from (*) in one movement, I must do:

replace_val(list1, 'the', 'ART')
replace_val(list1, 'quick', 'VERB')
replace_val(list1, 'fox', 'ANIMAL')

Thus, my question is: How can I replace efficiently in one movement all the new tuples from (*) in order to get?:

[('the', 'ART'), ('over','1'), ('quick','VERB'),('fox', 'ANIMAL'), ('brown', 'd'), ('dog','e'), ('jumps','2')]

Upvotes: 0

Views: 63

Answers (3)

Iron Fist
Iron Fist

Reputation: 10951

If you want to do it the simple and plain way, here is one:

>>> list1 = [('the', 'a'), ('over','1'), ('quick','b'),('fox', 'c'), ('brown', 'd'), ('dog','e'), ('jumps','2')]
>>> 
>>> list2 = [('the', 'ART'), ('quick', 'VERB'), ('fox', 'ANIMAL')]
>>> 
>>> list3 = []
>>> d1 = dict(list1)
>>> d2 = dict(list2)
>>> 
>>> for k in d1:
        if k not in d2:
            list3.append((k, d1[k]))
        else :
            list3.append((k, d2[k]))


>>> list3
[('jumps', '2'), ('dog', 'e'), ('brown', 'd'), ('the', 'ART'), ('fox', 'ANIMAL'), ('over', '1'), ('quick', 'VERB')]

Creating a dictionary from your list of tuples will make lookup along both lists very quick and easy, then all you have to do is take the corresponding values matching your search criteria.

Upvotes: 1

SarathSprakash
SarathSprakash

Reputation: 4624

I think this would help.

list1 = [('the', 'a'), ('over','1'), ('quick','b'),('fox', 'c'), ('brown', 'd'), ('dog','e'), ('jumps','2')]
list2 = [('the', 'a'), ('over','1'), ('quick','b'),('fox', 'c'), ('brown', 'Dog'), ('dog','e'), ('jumps','2')]
z=(dict(dict(list1),**dict(list2)).items())
print(z)

DEMO

Upvotes: 2

宏杰李
宏杰李

Reputation: 12168

list1 = [('the', 'a'), ('over','1'), ('quick','b'),('fox', 'c'), ('brown', 'd'), ('dog','e'), ('jumps','2')]
dict1 = dict(list1)
print(dict1)
dict1['the'] = 'ART'
dict1['quick'] = 'VERB'
dict1['fox'] = 'ANIMAL'
print(dict1)
list2 = [i for i in dict1.items()]
# or
list2 = list(dict1.items())

print(list2)

out:

{'the': 'a', 'fox': 'c', 'brown': 'd', 'dog': 'e', 'quick': 'b', 'jumps': '2', 'over': '1'}
{'the': 'ART', 'fox': 'ANIMAL', 'brown': 'd', 'dog': 'e', 'quick': 'VERB', 'jumps': '2', 'over': '1'}
[('the', 'ART'), ('fox', 'ANIMAL'), ('brown', 'd'), ('dog', 'e'), ('quick', 'VERB'), ('jumps', '2'), ('over', '1')]

Upvotes: 1

Related Questions