Given the following list of tuples:
l=[((459301.5857207412, 443923.4365563169),
(458772.4179957388, 446370.8372844439))]
And the following dictionary:
mydict={0: (459301.5857207412, 443923.4365563169),
25: (458772.4179957388, 446370.8372844439)}
How could I create a new list where the tuple contains the key in mydict associated with the values of the tuple itself?
The result would be, given the two examples above:
mapped=[(0,25)]
If
l=[((459301.5857207412, 443923.4365563169),
(458772.4179957388, 446370.8372844439))]
mydict={0: (459301.5857207412, 443923.4365563169),
25: (458772.4179957388, 446370.8372844439)}
I suppose I could generalize your simple case with this oneliner:
[tuple(k for k,v in mydict.items() if v in sl) for sl in l]
result:
[(0, 25)]
Note: for better performance, it would be better to pre-process l to create sets inside like this so lookup with in is faster (tuples are immutable/hashable, so let's take advantage of it):
l = [set(x) for x in l]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With