Fred Barclay
Fred Barclay

Reputation: 834

How to delete a tuple in a list in a list:

Say I have a list

dist= [[(0.0, 1.0)], [(20.0, 0.8563799085248148)], [(40.0, 0.8371216462519347)], [(60.0, 0.9282032302755089)], [(80.0, 7.8429970322236064), (80.0, 1.2045483557883576)], [(100.0, 3.753460385470896), (100.0, 2.070863609380179)], [(120.0, 2.6794919243112276), (120.0, 12.92820323027545)], [(140.0, 2.298981118867903)], [(160.0, 2.250827351906659)], [(180.0, 2.4999999999999996), (180.0, 6.000000000000004)], [(200.0, 3.2523178818773006), (200.0, 3.0522653889161626)], [(220.0, 5.622391569468206), (220.0, 2.226834844885431)], [(240.0, 37.32050807568848), (240.0, 1.9366857335569074)], [(260.0, 1.9181147622136665)], [(280.0, 2.1576718089133085)], [(300.0, 2.85976265663383)], [(320.0, 2.9627929206431776), (320.0, 5.162096782237789)], [(340.0, 1.4051274947736847), (340.0, 69.47032761621178)]]

Notice that I have some elements in the list that share a first element (Sorry for incorrect terminology.) For example, dist[4] is [(80.0, 7.8429970322236064), (80.0, 1.2045483557883576)]; and the 80 is in both tuples.

Now I want to delete the tuple whose tup[1] element is larger. So in this case, I want to delete (80.0, 7.8429970322236064) because 7.84299... is larger than 1.20454....

After the process is completed, dist[4] would only be [(80.0, 1.2045483557883576)]

I've tried something like this

for e in range(len(dist)):
    if len(dist[e]) >= 2:
            for f in range(len(dist[e])):
                del max(dist[e][f])

But it returns this:

>SyntaxError: can't delete function call

Upvotes: 1

Views: 474

Answers (5)

marmeladze
marmeladze

Reputation: 6562

with deep iteration :)

for pairs in dist:
    if len(pairs)>1:
            for tuples in pairs:
                    if sum(tuples[0])>sum(tuples[1]): 
                        tuples.remove(tuples[0])
                    else: 
                        tuples.remove(tuples[1])

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

dist= [[(0.0, 1.0)], [(20.0, 0.8563799085248148)], [(40.0, 0.8371216462519347)], [(60.0, 0.9282032302755089)], [(80.0, 7.8429970322236064), (80.0, 1.2045483557883576)], [(100.0, 3.753460385470896), (100.0, 2.070863609380179)], [(120.0, 2.6794919243112276), (120.0, 12.92820323027545)], [(140.0, 2.298981118867903)], [(160.0, 2.250827351906659)], [(180.0, 2.4999999999999996), (180.0, 6.000000000000004)], [(200.0, 3.2523178818773006), (200.0, 3.0522653889161626)], [(220.0, 5.622391569468206), (220.0, 2.226834844885431)], [(240.0, 37.32050807568848), (240.0, 1.9366857335569074)], [(260.0, 1.9181147622136665)], [(280.0, 2.1576718089133085)], [(300.0, 2.85976265663383)], [(320.0, 2.9627929206431776), (320.0, 5.162096782237789)], [(340.0, 1.4051274947736847), (340.0, 69.47032761621178)]]
from operator import itemgetter
for sub in dist:
    if len(sub) > 1:
        mx = max(sub,key=itemgetter(1))
        sub.remove(mx)

from pprint import pprint as pp

pp(dist)

[[(0.0, 1.0)],
 [(20.0, 0.8563799085248148)],
 [(40.0, 0.8371216462519347)],
 [(60.0, 0.9282032302755089)],
 [(80.0, 1.2045483557883576)],
 [(100.0, 2.070863609380179)],
 [(120.0, 2.6794919243112276)],
 [(140.0, 2.298981118867903)],
 [(160.0, 2.250827351906659)],
 [(180.0, 2.4999999999999996)],
 [(200.0, 3.0522653889161626)],
 [(220.0, 2.226834844885431)],
 [(240.0, 1.9366857335569074)],
 [(260.0, 1.9181147622136665)],
 [(280.0, 2.1576718089133085)],
 [(300.0, 2.85976265663383)],
 [(320.0, 2.9627929206431776)],
 [(340.0, 1.4051274947736847)]]

Upvotes: 1

B. M.
B. M.

Reputation: 18638

[[min(l)] for l in dist]

does the job, because min works fine on tuples: (80,1)<(80,2).

Upvotes: 1

Brionius
Brionius

Reputation: 14098

Here's a solution using reduce:

dist= [[(0.0, 1.0)], [(20.0, 0.8563799085248148)], [(40.0, 0.8371216462519347)], [(60.0, 0.9282032302755089)], [(80.0, 7.8429970322236064), (80.0, 1.2045483557883576)], [(100.0, 3.753460385470896), (100.0, 2.070863609380179)], [(120.0, 2.6794919243112276), (120.0, 12.92820323027545)], [(140.0, 2.298981118867903)], [(160.0, 2.250827351906659)], [(180.0, 2.4999999999999996), (180.0, 6.000000000000004)], [(200.0, 3.2523178818773006), (200.0, 3.0522653889161626)], [(220.0, 5.622391569468206), (220.0, 2.226834844885431)], [(240.0, 37.32050807568848), (240.0, 1.9366857335569074)], [(260.0, 1.9181147622136665)], [(280.0, 2.1576718089133085)], [(300.0, 2.85976265663383)], [(320.0, 2.9627929206431776), (320.0, 5.162096782237789)], [(340.0, 1.4051274947736847), (340.0, 69.47032761621178)]]

dist_reduced = [reduce(lambda a, b: a if a[1]>b[1] else b, elements) for elements in dist]

print dist_reduced

>> [(0.0, 1.0), (20.0, 0.8563799085248148), (40.0, 0.8371216462519347), (60.0, 0.9282032302755089), (80.0, 7.8429970322236064), (100.0, 3.753460385470896), (120.0, 12.92820323027545), (140.0, 2.298981118867903), (160.0, 2.250827351906659), (180.0, 6.000000000000004), (200.0, 3.2523178818773006), (220.0, 5.622391569468206), (240.0, 37.32050807568848), (260.0, 1.9181147622136665), (280.0, 2.1576718089133085), (300.0, 2.85976265663383), (320.0, 5.162096782237789), (340.0, 69.47032761621178)]

Upvotes: -1

ferhatelmas
ferhatelmas

Reputation: 3978

Import operator to supply compare function to min.

>>> from operator import itemgetter

Iterate over the list and for each item which is also a list, get the minimum of it according to second element.

>>> [min(e, key=itemgetter(1)) for e in dist]

Upvotes: 1

Related Questions