简体   繁体   English

显示数字是否在 pandas 中最多 3 个位置是连续的

[英]display if the numbers are consecutive for upto 3 positions exactly in pandas

I have a df "Index_label", which has only one column(name: column1) which contain numbers.我有一个 df“Index_label”,它只有一个包含数字的列(名称:column1)。

if we have consecutive position ==3.如果我们有连续的 position ==3。 i would want it as out put我想要它作为输出

column1第 1 列

25 25

26------------consecutive upto 2 positions 26------------连续最多2个位置

110 110

111 111

112 112

113---------------------consecutive upto 4 positions 113---------------------连续最多4个位置

455 455

456 456

457---------------------consecutive upto 3 positions 457---------连续最多3个位置

desired o/p:所需的o / p:

110 110

111 111

112 112

455 455

456 456

457 457

You can use this code (assuming you get the numbers as an iterable of some kind):您可以使用此代码(假设您将数字作为某种迭代):

def getConsecutive(numbers):
  consecutive = []                                 # all 3-consecutive numbers
  sub_series = []                                  # current sub-series of consecutive numbers

  for number in numbers:

    if len(sub_series) == 0:                       # if the current sub-series is empty
      sub_series.append(number)                    # add the current number to it
    else:
      prev = sub_series[-1]         
      if number != prev+1:                         # if the current number is not consecutive
        if len(sub_series) >=3:                    # if there are at least 3 numbers in the sub_series
           consecutive.extend(sub_series[0:3])     # add the first 3 of the sub-series
        sub_series.clear()                         # clear the sub-series
      sub_series.append(number)                    # add the current number 

  if len(sub_series) >=3:                          # check if a 3-consecutive sub series is found
    consecutive.extend(sub_series[0:3])            # add it in

  return consecutive


numbers1 = [25,26,110,111,112,113,455,456,457]
numbers2 = [110, 111, 112, 113, 114, 115, 116, 117]
print(getConsecutive(numbers1))         # output = [110, 111, 112, 455, 456, 457]
print(getConsecutive(numbers2))         # output = [110, 111, 112]

This works (I think?), using a while loop and jumping up the count when a consecutive series is found:这有效(我认为?),使用while循环并在找到连续系列时增加计数:

def three_in_a_row(array):
    output=[]
    c = 0  
    while c < len(array)-2: 
        if (array[c] + 1 == array[c+1]) and (array[c] + 2 == array[c+2]): 
            output += [array[c],array[c+1],array[c+2]] 
            d = 1
            while (array[c] + d == array[c+d]):
                d+=1
                if (d+c) >= len(array):
                    break
            c += d
        else:
            c+=1
    return output

print(three_in_a_row([1,2,3,4,5,6,7,8,9]))
print(three_in_a_row([1,2,3,7,8,9]))
print(three_in_a_row([1,8,9,4,2,4,5,6,7,8]))

Output: Output:

[1, 2, 3]
[1, 2, 3, 7, 8, 9]
[4, 5, 6]

Fun exercise!有趣的运动!

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

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