Python在列表中查找n个连续的数字

4

我想知道如何在我的列表中查找一行连续数字是否有特定数量的连续数字,例如:

例如,如果我正在寻找两个1,则:

list = [1, 1, 1, 4, 6] #original list
list = ["true", "true", 1, 4, 6] #after my function has been through the list.

如果我正在寻找三个1,则:

list = [1, 1, 1, 4, 6] #original list
list = ["true", "true", "true", 4, 6] #after my function has been through the list.

我尝试过:

list = [1, 1, 2, 1]

1,1,1 in list #typed into shell, returns "(1, 1, True)"

任何帮助将不胜感激,我主要想了解正在发生什么,并且如何检查列表中的下一个元素是否与前x个元素相同。

7
不要将“list”用作变量名。这会重新绑定同名的内置函数,从而导致各种微妙的错误。 - Tim Pietzcker
这不是“连续数字”一词的含义。 连续意味着一个接一个地按顺序跟随。 因此,“3, 4, 5, 6”是一组连续数字。 您似乎正在寻找重复数字。 - Bill M.
3个回答

11

list赋值是一个不好的想法。请使用其他名称。

要找到连续相等值的最大数量,可以使用itertools.groupby

>>> import itertools
>>> l = [1, 1, 1, 4, 6]
>>> max(len(list(v)) for g,v in itertools.groupby(l)) 
3

仅搜索连续的1:

>>> max(len(list(v)) for g,v in itertools.groupby(l, lambda x: x == 1) if g) 
3

好的,那很有道理。但我只想找出列表中是否有n个连续的1紧挨着彼此,而不是分开的。然后,我想更改旧列表,并将这些n个连续的1更改为“true”,而不是1。 - code_by_night

1
>>> def find_repeats(L, num_repeats):
...     idx = 0
...     while idx < len(L):
...         if [L[idx]]*num_repeats == L[idx:idx+num_repeats]:
...             L[idx:idx+num_repeats] = [True]*num_repeats
...             idx += num_repeats
...         else:
...             idx += 1
...     return L
... 
>>> L=[1,1,1,4,6]
>>> print find_repeats(L, 2)
[True, True, 1, 4, 6]
>>> L=[1,1,1,4,6]
>>> print find_repeats(L, 3)
[True, True, True, 4, 6]
>>> 

这是一个版本,它允许您指定应匹配的数字并在第一次替换后停止。
>>> def find_repeats(L, required_number, num_repeats, stop_after_match=False):
...     idx = 0
...     while idx < len(L):
...         if [required_number]*num_repeats == L[idx:idx+num_repeats]:
...             L[idx:idx+num_repeats] = [True]*num_repeats
...             idx += num_repeats
...             if stop_after_match:
...                 break
...         else:
...             idx += 1
...     return L
... 
>>> L=[1,1,1,4,6]
>>> print find_repeats(L, 1, 2)
[True, True, 1, 4, 6]
>>> L=[1,1,1,4,6]
>>> print find_repeats(L, 1, 3)
[True, True, True, 4, 6]
>>> L=[1,1,1,4,4,4,6]
>>> print find_repeats(L, 1, 3)
[True, True, True, 4, 4, 4, 6]
>>> L=[1,1,1,4,4,4,6]
>>> print find_repeats(L, 4, 3)
[1, 1, 1, True, True, True, 6]

这太棒了,甚至比另一个更短。 我该如何使它只找到第一组重复。例如:num_repeats = 3 L = [1,1,1,2,4,1,1,1] 返回[True,True,True,2,4,1,1,1]我还设置了单个数字的搜索。例如:def(L, required_number, num_repeats) 如果required_number = 1 那么它只会将连续的1改为True 如果required_number = 4 那么它只会将连续的4改为True谢谢 - code_by_night
如果您尝试以下操作,则stop_after_match将失败: L=[1,1,4,4,4,1,4,4,4,6] print find_repeats(L, 4, 3)除此之外,它是完美的! - code_by_night
@lost_in_code,“stop_after_match”是一个可选参数,因此如果您希望启用它,请使用“find_repeats(L,4,3,True)”。 - John La Rooy
啊,谢谢。我已经坐在这里调试了一个小时了,一直在想 stop_after_match 是做什么的。 - code_by_night

0

我不明白你想做什么,但我准备了一个快速而不是很好的脚本,但它可以满足你的需求。

def repeated(num, lyst):
 # the 'out' list will contain the array you are looking for
 out = []
 # go through the list (notice that you go until "one before
 # the end" because you peek one forward)
 for k in range(len(lyst)-1):
  if lyst[k] == lyst[k+1] == num:
    # if the numbers are equal, add True (as a bool, but you could
    # also pass the actual string "True", as you have it in your question)
    out.append(True)
  else:
   # if they are not the same, add the number itself
    out.append(lyst[k])
 # check the last element: if it is true, we are done (because it was the same as the
 # last one), if not, then we add the last number to the list (because it was not the
 # same)
 if out[-1] != True:
  out.append(lyst[-1])
 # return the list  
 return out

用法如下:

print repeated(1, [1, 1, 1, 4, 6])

这非常接近我想要的,除了一个事实,即如果列表是[1, 1, 1, 1, 2, 3],只有前三个1变为true。我会看看如何纠正这个问题。谢谢 - code_by_night
似乎它只将n-1个1更改为True。 因为对于一行中的最后一个“1”,它会检查下一个项目是否相同:P编辑: 通过添加以下内容进行修复: elif lyst[k] == lyst[k-1] == num: out.append(True)完美解决 - code_by_night

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接