繁体   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