python查找列表中多个数字之间的距离
我的输出如下所示:python查找列表中多个数字之间的距离,python,list,distance,elements,Python,List,Distance,Elements,我的输出如下所示: output=[[[], [], [], [1582, 2912, 3109, 5711], []], [[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], []], [[], [], [], [], [27, 574, 835, 1221, 1388, 1525, 1842, 2070, 2547, 3578, 3798, 3932, 4066, 4157, 4350, 4567, 47
output=[[[], [], [], [1582, 2912, 3109, 5711], []],
[[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], []],
[[], [], [], [], [27, 574, 835, 1221, 1388, 1525, 1842, 2070, 2547, 3578, 3798, 3932, 4066, 4157, 4350, 4567, 4709, 5176, 5564, 5744], [], []],
[[], [], [], [], []],
[[]],
[[], []],
[[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], [], [1452, 2120, 5628]],
[[3610], []],
[[], [], [], []],
[[3842], []],
[[1566], [3842], []],
[[5182, 5569], [], []],
[[], [3842], [], [], [1452, 2120, 5628]],
[[], [], []],
[[]],
[[], [377, 576, 2682, 3071, 5760], [900, 1723, 2658, 3076], []],
[[], []],
[[], [], [], [], [1452, 2120, 5628]],
[[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], []]]
>>> dis=[]
>>> for i in range(len(output)):
for j in output[i]:
if any(abs(num-numb) for num in output[i] for numb in output[i+1]):
di=abs(num-numb)
dis.append(di)
对于输出的每一行,我需要找到一个列表中的数字到另一个列表中的数字的距离的所有可能组合。例如,对于行:
[[1566], [3842], []],
[[], [377, 576, 2682, 3071, 5760], [900, 1723, 2658, 3076], []],
我只需要找到距离(1566-3842),但对于行:
[[1566], [3842], []],
[[], [377, 576, 2682, 3071, 5760], [900, 1723, 2658, 3076], []],
我需要找到所有可能的距离组合。有人能告诉我一个快速的方法吗?非常感谢
我想做这样的事情:
output=[[[], [], [], [1582, 2912, 3109, 5711], []],
[[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], []],
[[], [], [], [], [27, 574, 835, 1221, 1388, 1525, 1842, 2070, 2547, 3578, 3798, 3932, 4066, 4157, 4350, 4567, 4709, 5176, 5564, 5744], [], []],
[[], [], [], [], []],
[[]],
[[], []],
[[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], [], [1452, 2120, 5628]],
[[3610], []],
[[], [], [], []],
[[3842], []],
[[1566], [3842], []],
[[5182, 5569], [], []],
[[], [3842], [], [], [1452, 2120, 5628]],
[[], [], []],
[[]],
[[], [377, 576, 2682, 3071, 5760], [900, 1723, 2658, 3076], []],
[[], []],
[[], [], [], [], [1452, 2120, 5628]],
[[], [1182, 1432, 2127, 2274, 2613, 3024, 3703, 4723, 5288, 5438], [], []]]
>>> dis=[]
>>> for i in range(len(output)):
for j in output[i]:
if any(abs(num-numb) for num in output[i] for numb in output[i+1]):
di=abs(num-numb)
dis.append(di)
我说的对吗?这个问题很有趣,谢谢你的代码片段。我会进行列表理解,但也会丢弃您不需要的任何空列表: 在psuedocode中:
for each line in your output:
remove the blank results
if there are 2 result sets,
then calculate all (x - y) combinations of distances
在Python中:
combinations = []
for line in output:
result = [i for i in line if i]
if len(result) > 1:
combinations.append([abs(x - y) for x in result[0] for y in result[1]])
combinations.append()
使用一个列表理解,它可以高效地(很好,就像Python一样高效地)运行我认为您要进行的计算看起来您的行是嵌套的,这意味着一行包含一定数量的子列表,每个子列表都包含一定数量的距离值,或者根本没有。看起来您需要整行的所有距离值组合
在这种情况下,对于任何行,都可以展平列表,然后使用itertools.combines
如果通过组合,表示行中所有值的所有可能对,则这意味着由r
表示的组合长度为2
dis = []
for row in output:
flatrow = sum(row, [])
for a, b in itertools.combinations(flatrow, 2):
di = abs(a - b)
if di:
dis.append(di)
您可能正在寻找: 输出:
377 to 900: 523
377 to 1723: 1346
377 to 2658: 2281
377 to 3076: 2699
576 to 900: 324
576 to 1723: 1147
576 to 2658: 2082
576 to 3076: 2500
2682 to 900: 1782
2682 to 1723: 959
...
谢谢,我会编辑这篇文章。稍微好一点;你可能想包括你用这种方法遇到了什么问题,为什么它不起作用。在描述中,你提到的每一行似乎都是独立的。但在代码中,您似乎在寻找连续行之间的组合。代码错了吗?