How to get a nested list filtered base on another list

Viewed 79

I need to get the following list filtered by the the index number 1 from each tuple inside the list of the list of each exterior tuple, As for instance... I have the following list

  distances = [('highway_bost321', [0.0, 10.174343253183386, 10.947746706490813, 7.187637514988234, 7.660483562939873, 10.622402335214636, 10.737785768990813, 10.566917715980832, 10.819389772897063, 12.03400784892136]), ('mountain_0871', [10.947746706490813, 0.83758544921875, 0.0, 5.838234191502578, 5.363256367154217, 1.3175048828125, 3.0810546875, 6.634500456993904, 0.78460693359375, 13.91981431878607]), ('opencountry_043', [7.660483562939873, 4.668136048210366, 5.363256367154217, 2.8142531243329643, 0.0, 5.347678752303554, 3.236628762987552, 5.377074226549635, 5.634096535101808, 13.405988675235129]), ('opencountry_053', [10.622402335214636, 0.73712158203125, 1.3175048828125, 5.876377140065916, 5.347678752303554, 0.0, 3.1134033203125, 6.11476575647307, 1.97711181640625, 
13.144871284931902]), ('opencountry_123', [10.737785768990813, 3.01690673828125, 3.0810546875, 5.560564920669245, 3.236628762987552, 3.1134033203125, 0.0, 5.71620618616057, 3.09417724609375, 13.597874214619402]), ('palace_019', [10.566917715980832, 5.890665007775154, 6.634500456993904, 3.7392389463104037, 5.377074226549635, 6.11476575647307, 5.71620618616057, 0.0, 6.493675414476205, 12.731745772059295]), ('volcano_010', [10.819389772897063, 1.294677734375, 0.78460693359375, 5.709877257908828, 5.634096535101808, 1.97711181640625, 3.09417724609375, 6.493675414476205, 0.0, 13.90083238519232]), ('waterfall03', [12.03400784892136, 13.17207261956732, 13.91981431878607, 13.095577523116825, 13.405988675235129, 13.144871284931902, 13.597874214619402, 12.731745772059295, 13.90083238519232, 0.0])]

I've written the following code

medoids = [1, 3]
distances = [tuple((n, [(value, i) for i, value in enumerate(values) if i in medoids])) for n, values in distances]

which is giving me an error for the first element, why I have no idea since I am filtering the elements in the medoids list [1,3] whith the following piece of code if i in medoids Without such filtering I would get the following list

[('highway_bost321', [(10.174343253183386, 1), (10.947746706490813, 2), (7.187637514988234, 3), (7.660483562939873, 4), (10.622402335214636, 5), (10.737785768990813, 6), (10.566917715980832, 7), (10.819389772897063, 8), (12.03400784892136, 9)]),
('mountain_0871', [(10.947746706490813, 0), (0.83758544921875, 1), (5.838234191502578, 3), (5.363256367154217, 4), (1.3175048828125, 5), (3.0810546875, 6), (6.634500456993904, 7), (0.78460693359375, 8), (13.91981431878607, 9)]),
('opencountry_043', [(7.660483562939873, 0), (4.668136048210366, 1), (5.363256367154217, 2), (2.8142531243329643, 3), (5.347678752303554, 5), (3.236628762987552, 6), (5.377074226549635, 7), (5.634096535101808, 8), (13.405988675235129, 9)]),
('opencountry_053', [(10.622402335214636, 0), (0.73712158203125, 1), (1.3175048828125, 2), (5.876377140065916, 3), (5.347678752303554, 4), (3.1134033203125, 6), (6.11476575647307, 7), (1.97711181640625, 8), (13.144871284931902, 9)]),
('opencountry_123', [(10.737785768990813, 0), (3.01690673828125, 1), (3.0810546875, 2), (5.560564920669245, 3), (3.236628762987552, 4), (3.1134033203125, 5), (5.71620618616057, 7), (3.09417724609375, 8), (13.597874214619402, 9)]),
('palace_019', [(10.566917715980832, 0), (5.890665007775154, 1), (6.634500456993904, 2), (3.7392389463104037, 3), (5.377074226549635, 4), (6.11476575647307, 5), (5.71620618616057, 6), (6.493675414476205, 8), (12.731745772059295, 9)]),
('volcano_010', [(10.819389772897063, 0), (1.294677734375, 1), (0.78460693359375, 2), (5.709877257908828, 3), (5.634096535101808, 4), (1.97711181640625, 5), (3.09417724609375, 6), (6.493675414476205, 7), (13.90083238519232, 9)]),
('waterfall03', [(12.03400784892136, 0), (13.17207261956732, 1), (13.91981431878607, 2), (13.095577523116825, 3), (13.405988675235129, 4), (13.144871284931902, 5), (13.597874214619402, 6), (12.731745772059295, 7), (13.90083238519232, 8)])]

What I am getting...

   [('highway_bost321', [(7.187637514988234, 3)]), 
    ('mountain_0871', [(0.83758544921875, 1), (5.838234191502578, 3)]),
    ('opencountry_043', [(2.8142531243329643, 3), (4.668136048210366, 1)]),
    ('opencountry_053', [(0.73712158203125, 1), (5.876377140065916, 3)]),
    ('opencountry_123', [(3.01690673828125, 1), (5.560564920669245, 3)]),
    ('palace_019', [(3.7392389463104037, 3), (5.890665007775154, 1)]),
    ('volcano_010', [(1.294677734375, 1), (5.709877257908828, 3)]),
    ('waterfall03', [(13.095577523116825, 3), (13.17207261956732, 1)])]

What I want to get

   [('highway_bost321', [(10.174343253183386, 1), (7.187637514988234, 3)]), 
    ('mountain_0871', [(0.83758544921875, 1), (5.838234191502578, 3)]),
    ('opencountry_043', [(2.8142531243329643, 3), (4.668136048210366, 1)]),
    ('opencountry_053', [(0.73712158203125, 1), (5.876377140065916, 3)]),
    ('opencountry_123', [(3.01690673828125, 1), (5.560564920669245, 3)]),
    ('palace_019', [(3.7392389463104037, 3), (5.890665007775154, 1)]),
    ('volcano_010', [(1.294677734375, 1), (5.709877257908828, 3)]),
    ('waterfall03', [(13.095577523116825, 3), (13.17207261956732, 1)])]
2 Answers
medoids = [1, 3]
filtered_lst = [
    (n, [(values[m], m) for m in medoids])
    for n, values in distances
]

You do not need to enumerate if you already know which index from the list you want which I am assuming is medoids. Then just nest the list comprehension to select the first element and specific elements from the list.

medoids = [1, 3]

out = [(i[0],[(i[1][x], x) for x in medoids]) for i in distances]

>> [('highway_bost321', [(10.174343253183386, 1), (7.187637514988234, 3)]),
 ('mountain_0871', [(0.83758544921875, 1), (5.838234191502578, 3)]),
 ('opencountry_043', [(4.668136048210366, 1), (2.8142531243329643, 3)]),
 ('opencountry_053', [(0.73712158203125, 1), (5.876377140065916, 3)]),
 ('opencountry_123', [(3.01690673828125, 1), (5.560564920669245, 3)]),
 ('palace_019', [(5.890665007775154, 1), (3.7392389463104037, 3)]),
 ('volcano_010', [(1.294677734375, 1), (5.709877257908828, 3)]),
 ('waterfall03', [(13.17207261956732, 1), (13.095577523116825, 3)])]

Edit

That being said, your original solution should have worked too though the additional methods of using tuple. Try renaming your variables to make sure you do not run it twice.

medoids = [1, 3]
out = [tuple((n, [(value, i) for i, value in enumerate(values) if i in medoids])) for n, values in distances]

out
>>[('highway_bost321', [(10.174343253183386, 1), (7.187637514988234, 3)]),
 ('mountain_0871', [(0.83758544921875, 1), (5.838234191502578, 3)]),
 ('opencountry_043', [(4.668136048210366, 1), (2.8142531243329643, 3)]),
 ('opencountry_053', [(0.73712158203125, 1), (5.876377140065916, 3)]),
 ('opencountry_123', [(3.01690673828125, 1), (5.560564920669245, 3)]),
 ('palace_019', [(5.890665007775154, 1), (3.7392389463104037, 3)]),
 ('volcano_010', [(1.294677734375, 1), (5.709877257908828, 3)]),
 ('waterfall03', [(13.17207261956732, 1), (13.095577523116825, 3)])]
Related