简体   繁体   English

如何检查列表是否包含连续数字 Python

[英]How to check if list contains consecutive numbers Python

I have below list:我有以下列表:

l = [1, 2, 3, 4, 10, 11, 12]

By looking at the above list, we can say it's not consecutive.通过查看上面的列表,我们可以说它不是连续的。 In order to find that using python, we can use below line of code:为了找到使用 python,我们可以使用以下代码行:

print(sorted(l) == list(range(min(l), max(l)+1)))
# Output: False

This gives output False because 5, 6, 7, 8, 9 are missing.这给出了 output False 5, 6, 7, 8, 9 I want to further extend this functionality to check how many integers are missing.我想进一步扩展此功能以检查缺少多少整数。 Also to note, no duplicates are allowed in the list.另请注意,列表中不允许重复。 For ex:例如:

l = [1, 2, 3, 4, 10, 11, 12, 14]

output of above list should be [5, 1] because 5 integers are missing between 4 and 10 and 1 is missing between 12 and 14上面列表的 output 应该是[5, 1]因为410之间缺少5整数,而1214之间缺少1

This answers the question from the comments of how to find out how many are missing at multiple points in the list.这回答了评论中的问题,即如何找出列表中多个点缺少多少。 Here we assume the list arr is sorted and has no duplicates:这里我们假设列表arr已排序并且没有重复项:

it1, it2 = iter(arr), iter(arr)
next(it2, None) # advance past the first element
counts_of_missing = [j - i - 1 for i, j in zip(it1, it2) if j - i > 1]
total_missing = sum(counts_of_missing)

The iterators allow us to avoid making an extra copy of arr .迭代器允许我们避免制作arr的额外副本。 If we can be wasteful of memory, omit the first two lines and change zip(it1, it2) to zip(arr, arr[1:]) :如果我们可以浪费 memory,请省略前两行并将zip(it1, it2)更改为zip(arr, arr[1:])

counts_of_missing = [j - i - 1 for i, j in zip(arr, arr[1:]) if j - i > 1]

I think this will help you我想这会对你有所帮助

L = [1, 2, 3, 4, 10, 11, 12, 14]
C = []
D = True
for _ in range(1,len(L)):
    if L[_]-1!=L[_-1]:
        C.append(L[_]-L[_-1]-1)
        D = False
print(D)
print(C)

Here I have checked that a number at ith index minus 1 is equal to its previous index.在这里,我检查了ith index minus 1是否等于其先前的索引。 if not then D = false and add it to list如果不是,则 D = false 并将其添加到列表中

here is my attempt:这是我的尝试:

from itertools import groupby

l = [1, 2, 3, 4, 10, 11, 12, 14]

not_in = [i not in l for i in range(min(l),max(l)+1)]
missed = [sum(g) for i,g in groupby(not_in) if i]

>>> missed
'''
[5, 1]

Total number of missing integers缺失整数的总数

If you assume that arr contains no duplicates, then you can write directly:如果假设arr不包含重复项,则可以直接编写:

total_missing = max(arr) - min(arr) + 1 - len(arr)

The first term, max(arr) - min(arr) + 1 , is the total number of integers in interval [ min(arr) , max(arr) ].第一项max(arr) - min(arr) + 1是区间 [ min(arr) , max(arr) ] 中的整数总数。 The second term, len(arr) , is the actual number of integers in the array.第二项len(arr)是数组中整数的实际数量。

The difference between these two terms is the number of missing integers.这两项之间的区别在于缺失整数的数量。

Note that:注意:

  • if arr is sorted, then max(arr) is equal to arr[-1] and min(arr) is equal to arr[0] ;如果arr已排序,则max(arr)等于arr[-1]并且min(arr)等于arr[0]
  • if arr might contain duplicates, the above remains true if you replace len(arr) with len(set(arr)) .如果arr可能包含重复项,则如果将len(arr)替换为len(set(arr)) ,则上述内容仍然正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM