【问题标题】:How to get a list of tuples with some value based on its minimum value - Only with native libraries from Python如何根据最小值获取具有某些值的元组列表 - 仅使用 Python 中的本机库
【发布时间】:2021-07-20 07:32:32
【问题描述】:

我有以下代码...但是我需要获取对应的文件名的最小值:

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)

例如,我有以下示例数据K = 10,距离列表是:

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

我需要的是获得一个元组列表,例如下面的一个,其中名称之前的字符串是列表距离的每个元组的第一个元素的相应结果......这就是对应的名称的最小值...

如下表所示:

输出

  [[("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)]...]

请仅向我发送基于 Python 本地库的内容

【问题讨论】:

    标签: python list append list-comprehension min


    【解决方案1】:
    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)   
    

    【讨论】:

    • 感谢您回复我,但这并不是我想要的...请查看我为您提供的输出 ' [[("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.41714), ("ibis_142", 6.41712) ", 6.146172005),("highway_bost174", 8.194840093),("ibis_142", 5.149291993)]...]'
    • 好的,所以你想要一个按最小距离排序的元组列表()?我错过了什么吗,因为我认为我已经做到了。
    • 是的,看看第二个表...那里的名称对应于其对应的每个文件...在初始选择列中
    • 我建议运行以下命令:dist2 = distances[0:2] 后跟 sorted([(d[0], x) for d in dist2 for x in d[1]], key=lambda k: k[1])。我认为这提供了您所追求的输出。
    • 你想要distances中每个目的地组合的列表吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 2019-04-04
    相关资源
    最近更新 更多