[英]Find consecutive integers in a list
I am trying to find consecutive values from an unsorted list.我正在尝试从未排序的列表中查找连续值。 Experimental code below:
实验代码如下:
num = [8, 9, 4, 1, 2, 3]
#(num[0+1]) next value
for i in range(len(num)-1): # not using -1 will cause index error
if num[i]+1==num[i+1]:
print('Con',num[i])
Problem: I am unable to get the last value with this current code.问题:我无法使用当前代码获取最后一个值。 My output excludes the last value.
我的输出不包括最后一个值。 Here is what I get (no 9 or no 3):
这是我得到的(没有 9 或没有 3):
Con 8
Con 1
Con 2
I have seen a few complex solutions which were a little difficult for me to understand.我见过一些复杂的解决方案,我有点难以理解。 Is it possible to tweak the for loop part a little and get the entire sequence?
是否可以稍微调整 for 循环部分并获得整个序列? Thanks a lot.
非常感谢。
You can use the function groupby
:您可以使用功能
groupby
:
from itertools import groupby
num = [8, 9, 4, 1, 2, 3]
# Enumerate and get differences between counter—integer pairs
# Group by differences (consecutive integers have equal differences)
gb = groupby(enumerate(num), key=lambda x: x[0] - x[1])
# Repack elements from each group into list
all_groups = ([i[1] for i in g] for _, g in gb)
# Filter out one element lists
list(filter(lambda x: len(x) > 1, all_groups))
# [[8, 9], [1, 2, 3]]
This is because you only check the next number.这是因为您只检查下一个数字。 When you want the second number (like 9 or 3), you have to include a check for the previous number too.
当您想要第二个数字(如 9 或 3)时,您还必须检查前一个数字。 This will make the
if
a bit longer, but it'll work.这将使
if
更长一点,但它会起作用。
num=[8,9,4,1,2,3]
for i in range(len(num)):
if (
( # check for the next number
i + 1 != len (num) and # don't check the end of the list
num[i]+1==num[i+1]
) or ( # check for the previous number
i != 0 and # don't check before the list
num [i-1] == num [i] - 1
)
): print('Con',num[i])
Also, I had to remove the -1
in your range, because I already do a manual check, and as pointed out, this prvented 3 from being shown.另外,我必须删除您范围内的
-1
,因为我已经进行了手动检查,并且正如所指出的那样,这阻止了 3 的显示。
Your code only tests in one direction (being followed by a consecutive number).您的代码只测试一个方向(后面跟着一个连续的数字)。 For the full sequence you have to test in both direction.
对于完整的序列,您必须在两个方向上进行测试。
num=[8,9,4,1,2,3]
assert(len(num) > 1)
for i, n in enumerate(num):
if i != 0:
if n == num[i-1] + 1:
print("Con", n)
continue
if i != len(num) - 1:
if n == num[i+1] - 1:
print("Con", n)
One way would be to print both numbers when you found them to be consecutive, but also check that the one at index i-1
was not in the consecutive list as well so that the number at index i
is not printed twice:一种方法是在您发现它们是连续的时打印这两个数字,但还要检查索引
i-1
处的数字是否不在连续列表中,以便索引i
处的数字不会打印两次:
num = [8, 9, 4, 1, 2, 3]
for i in range(len(num)-1): # not using -1 will cause index error
if num[i] + 1 == num[i + 1]:
if i == 0 or (i - 1 >= 0 and num[i - 1] != num[i] - 1):
print('Con', num[i])
print('Con', num[i + 1])
Could try with a more complex list as well:也可以尝试使用更复杂的列表:
num = [8, 9, 4, 1, 2, 3, 4, 4, 8, 9, 1, 2, 3, 0, 1, 5, 6, 1]
for i in range(len(num)-1): # not using -1 will cause index error
if num[i] + 1 == num[i + 1]:
if i == 0 or (i - 1 >= 0 and num[i - 1] != num[i] - 1):
print('Con', num[i])
print('Con', num[i + 1])
num = [8, 9, 4, 1, 2, 3]
def con(rng, pos=0):
if pos < len(rng):
if (pos > 0 and rng[pos]-1 == rng[pos-1]) or (pos < len(rng) -1 and rng[pos]+1 == rng[pos+1]):
print("con", rng[pos])
con(rng, pos+1)
con(num)
edit: this is solution is based on concurrent function, and needs only the list as argument.编辑:这是基于并发功能的解决方案,只需要列表作为参数。 As long as they are within lower-/upperbound of list, the function will check if (previous number)-1 or (next number)+1 are equal (this number) output will be: con 8 con 9 con 1 con 2 con 3
只要它们在列表的下限/上限范围内,该函数将检查(上一个数字)-1 或(下一个数字)+1 是否相等(这个数字)输出将是: con 8 con 9 con 1 con 2 con 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.