簡體   English   中英

找出列表元素的所有可能方式的差異

[英]Find the differences in all possible ways of list elements

我在列中列出了升序號碼:

 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)]  

我期待一個列表中的所有輸出值。

您可以使用兩個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]]

這聽起來像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])]

使用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]

如果您需要一個列表中的所有結果,您可以使用函數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]

這可以使用兩個循環來解決,外部循環將選擇列表中的一個元素,內部循環將計算外部值與列表的所有其他項的差異:

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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM