I have a list of tuples and need to delete tuples if its 1st item is matching with 1st item of other tuples in the list. 3rd item may or may not be the same, so I cannot use set (I have seen this question - Grab unique tuples in python list, irrespective of order and this is not same as my issue)
For eg if I got a
as:
[(0, 13, 'order1'), (14, 27, 'order2'), (14, 27, 'order2.1'),
(0, 13, 'order1'), (28, 41, 'order3')]
I want the output as:
[(14, 27, 'order2'), (0, 13, 'order1'), (28, 41, 'order3')]
I am getting the desired output using below code.
for e, i in enumerate(a):
r = [True if i[0] == k[0] and e != j else False for j, k in enumerate(a)]
if any(r):
a.pop(e)
pprint(a)
Is there a better or more pythonic way to achieve the same?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…