如何删除列表中列表中的元组:

时间:2015-04-26 20:36:54

标签: python 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)]]

请注意,我在列表中有一些共享第一个元素的元素(对不正确的术语表示抱歉。)例如,dist[4][(80.0, 7.8429970322236064), (80.0, 1.2045483557883576)]; 80是两个元组。

现在我要删除tup[1]元素较大的元组。所以在这种情况下,我想删除(80.0, 7.8429970322236064),因为7.84299 ...大于1.20454 ....

完成此过程后,dist[4]只会是[(80.0, 1.2045483557883576)]

我尝试过这样的事情

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

但它返回了这个:

>SyntaxError: can't delete function call

5 个答案:

答案 0 :(得分:1)

导入operator以将比较功能提供给min。

>>> from operator import itemgetter

迭代列表,对于也是列表的每个项目,根据第二个元素获得最小值。

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

答案 1 :(得分:1)

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)]]

答案 2 :(得分:1)

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

完成这项工作,因为min在元组上工作正常:(80,1)<(80,2)

答案 3 :(得分:0)

深度迭代:)

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])

答案 4 :(得分:-1)

以下是使用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)]