Search code examples
pythonlistappendlist-comprehensionmin

How to get a list of tuples with some value based on its minimum value - Only with native libraries from Python


I have the following code... But I need to get the corresponding filename for the minimum value:

import itertools

arrayFiles = []
for subset in itertools.combinations(distances, 2):
  array = []
  for k in range(K):
    array.append(min([(n[1][k]) for n in subset]))
  arrayFiles.append(array)

For instance, I have the following sample data K = 10 and distances list is:

distances = [('highway_bost174', [0.0, 8.708170812, 4.088197921, 11.366319879999999, 12.638763287, 11.078233943, 10.025102839, 8.415467337, 8.194840093, 13.455056175000001]),
             ('ibis_142', [8.708170812, 0.0, 10.518235207, 7.668395996, 10.522399903, 7.302185059, 6.417022705, 6.146172005, 10.448354985, 5.149291993]),
             ('street_par88', [4.088197921, 10.518235207, 0.0, 11.135904053, 11.472831274, 10.691568116, 9.663827636, 10.659660884000001, 9.392413013999999, 12.586018896]),
             ('opencountry_241', [11.366319879999999, 7.668395996, 11.135904053, 0.0, 13.314941407, 2.754882813, 3.998626709, 9.028326501, 12.145703089000001, 8.675354002999999]),
             ('waterfall23', [12.638763287, 10.522399903, 11.472831274, 13.314941407, 0.0, 12.665527344000001, 11.406341552, 12.6048929, 11.43774673, 8.79888916]),
             ('field26', [11.078233943, 7.302185059, 10.691568116, 2.754882813, 12.665527344000001, 0.0, 3.349212646, 8.966176812, 11.827669236000002, 8.203674316]),
             ('mountain_030', [10.025102839, 6.417022705, 9.663827636, 3.998626709, 11.406341552, 3.349212646, 0.0, 8.78585096, 11.994283939999999, 7.7325744620000005]),
             ('horse_081', [8.415467337, 6.146172005, 10.659660884000001, 9.028326501, 12.6048929, 8.966176812, 8.78585096, 0.0, 8.054160893999999, 11.093641082000001]),
             ('bison_052', [8.194840093, 10.448354985, 9.392413013999999, 12.145703089000001, 11.43774673, 11.827669236000002, 11.994283939999999, 8.054160893999999, 0.0, 12.869559482]),
             ('ibis_040', [13.455056175000001, 5.149291993, 12.586018896, 8.675354002999999, 8.79888916, 8.203674316, 7.7325744620000005, 11.093641082000001, 12.869559482, 0.0])]
highway_bost174 ibis_142 street_par88 opencountry_241 waterfall23 field26 mountain_030 horse_081 bison_052 ibis_040
highway_bost174 0 8.708170812 4.088197921 11.366319880 12.63876329 11.07823394 10.02510284 8.415467337 8.194840093 13.45505618
ibis_142 8.708170812 0 10.518235207 7.668395996 10.5223999 7.302185059 6.417022705 6.146172005 10.44835499 5.149291993
street_par88 4.088197921 10.51823521 0. 11.135904053 11.47283127 10.69156812 9.663827636 10.65966088 9.392413014 12.5860189
opencountry_241 11.36631988 7.668395996 11.135904053 0. 13.31494141 2.754882813 3.998626709 9.028326501 12.14570309 8.675354003
waterfall23 12.63876329 10.5223999 11.472831274 13.314941407 0 12.66552734 11.40634155 12.6048929 11.43774673 8.79888916
field26 11.07823394 7.302185059 10.691568116 2.754882813 12.66552734 0 3.349212646 8.966176812 11.82766924 8.203674316
mountain_030 10.02510284 6.417022705 9.663827636 3.998626709 11.40634155 3.349212646 0 8.78585096 11.99428394 7.732574462
horse_081 8.415467337 6.146172005 10.65966088 9.028326501 12.6048929 8.966176812 8.78585096 0 8.054160894 11.09364108
bison_052 8.194840093 10.44835499 9.392413014 12.145703089 11.43774673 11.82766924 11.99428394 8.054160894 0 12.86955948
ibis_040 13.45505618 5.149291993 12.586018896 8.675354003 8.79888916 8.203674316 7.732574462 11.09364108 12.86955948 0

What I need is to get a list of tuples such as the one down bellow where the string which comes before the name is the corresponding result of the first element of each tuple of the list distances... That's it the correposding name of the min value...

The way you can see in the table down bellow:

enter image description here

OUTPUT

  [[("highway_bost174", 0), ("ibis_142",0),("highway_bost174", 4.088197921),("ibis_142", 7.668395996),("ibis_142", 10.5223999), ("ibis_142", 7.302185059), ( "ibis_142", 6.417022705), ("ibis_142", 6.146172005),("highway_bost174", 8.194840093),("ibis_142", 5.149291993)]...]

Please send me only stuff based on native libraries from Python


Solution

  • import itertools
    arrayFiles = []
    for subset in itertools.combinations(distances, 2):
        one = subset[0]
        two = subset[1]
        one_tup = [(subset[0][0], x) for x in subset[0][1]]
        two_tup = [(subset[1][0], x) for x in subset[1][1]]
        pair = [(o, two_tup[i]) for (i, o) in enumerate(one_tup)]
        res_list = []
        for e in pair:
            if e[1][1] > e[0][1]:
                res_list.append(e[0])
            else:
                res_list.append(e[1])
        arrayFiles.append(res_list)