Reputation: 875
I have two lists:
l = [
['0.064', 'facebook', '0'],
['0.019', 'twitt', '0'],
['0.018', 'netzwerk', '0'],
['0.016', 'nachricht', '0'],
['0.015', 'youtub', '0']
]
fl = [
['twitter', 'twitt', 1],
['youtube', 'youtub', 2],
['nachrichten', 'nachricht', 4]
]
As you can see the entries at index [1]
in list l
are stems, which I want to replace with values from list fl
.
So if both entries at index [1]
in list l
and fl
then I want to replace the entry at index [1]
in l
with the entry at index [0]
in fl
.
Expected result:
keys = [
['0.064', 'facebook', '0'],
['0.019', 'twitter', '0'],
['0.018', 'netzwerk', '0'],
['0.016', 'nachrichten', '0'],
['0.015', 'youtube', '0']
]
I tried something like this:
keys =[]
for l in l:
for i in fl:
if l[1] == i[1]:
i[1].replace(i[1], l[0])
keys.append(i)
But this does not work.
Can somebody help?
Upvotes: 0
Views: 93
Reputation: 26039
Your second list is better transformed to a dictionary and make use of O(1)
lookup:
l = [
['0.064', 'facebook', '0'],
['0.019', 'twitt', '0'],
['0.018', 'netzwerk', '0'],
['0.016', 'nachricht', '0'],
['0.015', 'youtub', '0']
]
fl = [
['twitter', 'twitt', 1],
['youtube', 'youtub', 2],
['nachrichten', 'nachricht', 4]
]
d = dict((x[1], x[0]) for x in fl)
keys = [[x[0], d.get(x[1], x[1]), x[2]] for x in l]
print(keys)
Upvotes: 2