簡體   English   中英

如果與前一個元素的差異小於值,則刪除列表中的元素

[英]Remove elements in a list if difference with previous element less than value

給定一個按升序排列的數字列表。 有必要只留下元素以獲得這樣一個列表,其中元素之間的差異大於或等於某個值(在我的情況下為 10)。

鑒於:

list = [10,15,17,21,34,36,42,67,75,84,92,94,103,115]

目標:

 list=[10,21,34,67,84,94,115]

您可以使用 while 循環和變量來跟蹤您當前正在查看的當前索引。 因此,從索引 1 開始,檢查該索引處的數字減去前一個索引中的數字是否小於 10。如果是,則刪除此索引但保持索引計數器相同,以便我們查看現在所在的下一個 num這個指數。 如果差異為 10 或更多,則增加索引以查看下一個數字。 我在循環中有一個額外的打印行,您可以刪除它只是為了顯示比較。

nums = [10, 15, 17, 21, 34, 36, 42, 67, 75, 84, 92, 94, 103, 115]

index = 1
while index < len(nums):
    print(f"comparing {nums[index-1]} with {nums[index]} nums list {nums}")
    if nums[index] - nums[index - 1] < 10:
        del nums[index]
    else:
        index += 1

print(nums)

輸出

comparing 10 with 15 nums list [10, 15, 17, 21, 34, 36, 42, 67, 75, 84, 92, 94, 103, 115]
comparing 10 with 17 nums list [10, 17, 21, 34, 36, 42, 67, 75, 84, 92, 94, 103, 115]
comparing 10 with 21 nums list [10, 21, 34, 36, 42, 67, 75, 84, 92, 94, 103, 115]
comparing 21 with 34 nums list [10, 21, 34, 36, 42, 67, 75, 84, 92, 94, 103, 115]
comparing 34 with 36 nums list [10, 21, 34, 36, 42, 67, 75, 84, 92, 94, 103, 115]
comparing 34 with 42 nums list [10, 21, 34, 42, 67, 75, 84, 92, 94, 103, 115]
comparing 34 with 67 nums list [10, 21, 34, 67, 75, 84, 92, 94, 103, 115]
comparing 67 with 75 nums list [10, 21, 34, 67, 75, 84, 92, 94, 103, 115]
comparing 67 with 84 nums list [10, 21, 34, 67, 84, 92, 94, 103, 115]
comparing 84 with 92 nums list [10, 21, 34, 67, 84, 92, 94, 103, 115]
comparing 84 with 94 nums list [10, 21, 34, 67, 84, 94, 103, 115]
comparing 94 with 103 nums list [10, 21, 34, 67, 84, 94, 103, 115]
comparing 94 with 115 nums list [10, 21, 34, 67, 84, 94, 115]
[10, 21, 34, 67, 84, 94, 115]

您可以在循環中建立列表。 從列表中的第一個數字開始。 跟蹤選擇在新列表中的最后一個數字。 僅當項目與最后選擇的數字相差至少目標數量時,才將項目添加到新列表中:

my_list = [10,15,17,21,34,36,42,67,75,84,92,94,103,115]

last_num = my_list[0]
new_list = [last_num]

for x in my_list[1:]:
    if x - last_num >= 10:
        new_list.append(x)
        last_num = x

print(new_list) #prints [10, 21, 34, 67, 84, 94, 115]

這個問題可以通過迭代初始值集並僅在滿足 x 條件差異時將它們添加到新列表中來相當簡單地解決。

此外,通過將此功能放入函數中,您可以輕松交換值或最小距離。

values = [10,15,17,21,34,36,42,67,75,84,92,94,103,115]

def foo(elements, distance):
  elements = sorted(elements) # sorting the user input
  new_elements = [elements[0]] # make a new list for output
  for element in elements[1:]: # Iterate over the remaining elements...
    if element - new_elements[-1] >= distance: 
      # this is the condition you described above
      new_elements.append(element)

  return new_elements

print(foo(values, 10))
# >>> [10, 21, 34, 67, 84, 94, 115]
print(foo(values, 5))
# >>> [10, 15, 21, 34, 42, 67, 75, 84, 92, 103, 115]

這里還有一些其他注意事項......

  • 我在處理之前對數組進行了sorted 您可能不想為您的特定應用程序這樣做,但這似乎是有道理的,因為您的示例數據已經排序。 如果您不想在構建列表之前對數據進行排序,您可以刪除我上面評論的行上的sorted

  • 我將函數命名為foo是因為我很懶,不想考慮名稱。 我強烈建議您給它一個更具描述性的名稱。

暫無
暫無

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

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