如何根据最小值获取具有某些值的元组列表 - 仅适用于 Python 的本机库
How to get a list of tuples with some value based on its minimum value - Only with native libraries from Python
我有下面的代码...但是我需要得到最小值对应的文件名:
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
我需要的是得到一个元组列表,例如下面的那个,其中名称前面的字符串是列表距离的每个元组的第一个元素的相应结果...就是这样最小值的相应名称...
您在下面的 table 中看到的方式:
输出
[[("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
中原生库的内容
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)
我有下面的代码...但是我需要得到最小值对应的文件名:
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 |
我需要的是得到一个元组列表,例如下面的那个,其中名称前面的字符串是列表距离的每个元组的第一个元素的相应结果...就是这样最小值的相应名称...
您在下面的 table 中看到的方式:
输出
[[("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
中原生库的内容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)