在列表元素的所有可能方式中找到差异

时间:2019-03-18 11:29:38

标签: python list

我在列中有一个升序列表:

 45
 59 
 68 
 79 
 89
 99

我想要以下模式中每个数字之间的差异。

Difference between first consecutive:
    59 - 45
    68 - 59
    79 - 68
    89 - 79
    99 - 89

Difference between second consecutive:
    68 - 45
    79 - 59
    89 - 68
    99 - 79

Difference between third consecutive:
    79 - 45

And so on...

我尝试了

 with open("file.xls", 'r', newline='') as report: 
     reader = csv.reader(report, delimiter='\t' )                                             
     for row in reader:                                                                       
         list5 = []                                                                           
         if row[0] == "chr5":                                                                 
             list5.append(row[1])                                                             

在将列表中的所有值附加后,我试图找出差异,但仅针对第一个连续元素

v = [list5[i+1]-list5[i] for i in range(len(list5)-1)]  

我希望所有输出值都在一个列表中。

5 个答案:

答案 0 :(得分:3)

您可以使用两个for来计算差异,并使用一个来增加它们之间的距离值,如下所示:

[[list5[i+j]-list5[i] for i in range(len(list5)-j)] for j in range(1, len(list5))]
# [[14, 9, 11, 10, 10], [23, 20, 21, 20], [34, 30, 31], [44, 40], [54]]

答案 1 :(得分:3)

对于zip来说,这似乎是绝佳的机会。

例如,以下代码循环遍历list5列表的两个不同版本:一个用于第一个(N-1)个元素,一个用于第二个到第N个元素:

result = []
for element_i, element_j in zip(list5[1:], list5[:-1]):
   result.append(element_i - element_j)

您可以通过列表理解获得相同的结果:

result = [(element_i - element_j) for element_i, element_j in zip(list5[1:], list5[:-1])]

答案 2 :(得分:3)

带有while和for循环的替代方法,即使不是最佳方法,也可能更容易理解:

l = [45, 59, 68, 79, 89, 99]
differences = []
max_diff = len(l) - 1
diff = 1
while diff <= max_diff:
  print(f"Consecutive elements with {diff} difference")
  for i in range(diff, len(l)):
    print(f"{l[i]} - {l[i-diff]} = {l[i]-l[i-diff]}")
    differences.append(l[i]-l[i-diff])
  diff += 1
print(f"differences: {differences}")

输出:

Consecutive elements with 1 difference
59 - 45 = 14
68 - 59 = 9
79 - 68 = 11
89 - 79 = 10
99 - 89 = 10
Consecutive elements with 2 difference
68 - 45 = 23
79 - 59 = 20
89 - 68 = 21
99 - 79 = 20
Consecutive elements with 3 difference
79 - 45 = 34
89 - 59 = 30
99 - 68 = 31
Consecutive elements with 4 difference
89 - 45 = 44
99 - 59 = 40
Consecutive elements with 5 difference
99 - 45 = 54
differences: [14, 9, 11, 10, 10, 23, 20, 21, 20, 34, 30, 31, 44, 40, 54]

答案 3 :(得分:2)

如果您需要所有结果在一个列表中,则可以使用函数combinations()

from itertools import combinations, starmap
from operator import sub

l = [45, 59, 68, 79, 89, 99]

l_ = sorted(l, reverse=True)
list(starmap(sub, combinations(l_, 2)))
# [10, 20, 31, 40, 54, 10, 21, 30, 44, 11, 20, 34, 9, 23, 14])

list(map(abs, starmap(sub, combinations(l, 2))))
# [14, 23, 34, 44, 54, 9, 20, 30, 40, 11, 21, 31, 10, 20, 10]

答案 4 :(得分:1)

这可以使用两个循环来解决,外部循环将选择列表的一个元素,内部循环将计算外部值与列表的所有其他项的差:

given_list=[45,59,68,79,89,99]
for i,v in enumerate(given_list):
    for j in range(i+1,len(given_list),1):
        print('{}-{} is:{}'.format(given_list[j],v,given_list[j]-v))