2010-05-28 363 views
3

我想知道如何查找列表中是否有一定數量的連續數字,例如Python在列表中找到n個連續的數字

例如,如果我找了兩個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'作爲變量名稱。它重新綁定同名的內置函數,從而導致各種微妙的錯誤... – 2010-05-28 07:16:32

+0

好吧,謝謝你:) – 2010-05-28 07:23:23

回答

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] 
+0

這是輝煌的,甚至比另一個更短。 我將如何製作它,因此它只能找到第一組重複。 例如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至真 如果required_number = 4 那就只能改變連續4對真 感謝 – 2010-05-29 07:44:40

+0

@lost_in_code,確保萬無一失。我添加了一個新的版本到我的回答 – 2010-05-29 22:11:54

+0

stop_after_match失敗,如果你嘗試: L = [1,1,4,4,4,1,4,4,4,6] print find_repeats(L,4,3) 除此之外它的完美! – 2010-05-30 00:10:35

10

指定給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 
+0

好的,這是有道理的。 但我只想知道在列表中是否有n個連續的1的右邊相鄰,而不是分離的,那麼我想更改舊的列表並將這n個連續的1更改爲「true」而不是1. – 2010-05-28 07:25:55

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]) 
+0

This是非常接近我想要的,除了事實如果列表[1,1,1,1,2,3]只有前三個1成爲真。 我會看看我能做些什麼來糾正這一點。 謝謝 – 2010-05-28 08:08:02

+0

似乎它只改變n-1 1爲真。 由於用於最後一列的 「1」,如果其下一個項目是相同的檢查:通過添加固定 ::P 編輯 的elif LYST [K] == LYST [K-1] == NUM​​: out.append(True) 作品魅力 – 2010-05-28 08:35:17

相關問題