是否有一種簡短的方法來檢測串列中帶有替代符號的最長子串列?
例如:
my_list = [-1, -0.5, 1, -3, 4, 5, 5, -1]
從-0.5到回傳 4 4?
這是我到目前為止所寫的內容,但我覺得還有更短的空間。
import numpy
my_list = [-1, -0.5, 1, -3, 4, 5, 5, -1]
# function that detects whether a list has alternate signs
# https://stackoverflow.com/questions/6451514/detect-alternating-signs
def is_alternating_signs(a):
return numpy.all(numpy.abs(numpy.diff(numpy.sign(a))) == 2)
# getting all sublists from the main list
sublists = []
for i in range(len(my_list) 1):
for j in range(i 1, len(my_list) 1):
sublists.append(my_list[i:j])
# detecting the longest sublist with alternate signs
max_list = 0
for sublist in sublists:
if is_alternating_signs(sublist) and len(sublist) > max_list:
max_list = len(sublist)
print(max_list)
uj5u.com熱心網友回復:
使用zip當前元素與下一個比較:
maxlen = 1
curlen = 1
for i, j in zip(l, l[1:]):
# if one conditions match
# increment curlen by 1
if (i < 0 and j > 0) or (i > 0 and j < 0):
curlen = 1
# break the alternative sign
# keep the highest value between maxlen and curlen
# reset curlen to 1
else:
maxlen = max(maxlen, curlen)
curlen = 1
maxlen = max(maxlen, curlen)
輸出:
>>> maxlen
4
uj5u.com熱心網友回復:
您可以使用 zip 來檢測交替中“中斷”的位置。然后將這些中斷組合到范圍內以找到交替值的最長連續性:
L = [-1, -0.5, 1, -3, 4, 5, 5, -1]
breaks = [i for i,(a,b) in enumerate(zip(L,L[1:]),1) if (a<0)==(b<0)]
longest = max((L[s:e] for s,e in zip([0] breaks,breaks [None])),key=len)
print(longest)
[-0.5, 1, -3, 4]
如果您只是在尋找連續的長度,您可以將 zip 結果轉換為 1 和 0 的字串,然后在 0 上拆分并測量最長的子字串:
max(map(len,"".join("01"[a*b<0] for a,b in zip(L,L[1:])).split('0'))) 1
4
uj5u.com熱心網友回復:
單回圈呢?
def max_alt_subseq_size(seq):
last_x = seq[0]
size = max_size = 1
for x in seq[1:]:
# use the fact that x * y < 0 iff x > 0 and y < 0 or x < 0 and y > 0
if last_x * x < 0:
size = 1
else:
# once the size of the alternating subsequence is found, we need to check if it is the largest
if size > max_size:
max_size = size
size = 1
last_x = x
# check on the final subsequence to see if it is the largest
if size > max_size:
max_size = size
return max_size
my_list = [-1, -0.5, 1, -3, 4, 5, 5, -1]
max_alt_subseq_size(my_list)
# 4
uj5u.com熱心網友回復:
一個人可以有(多個)完全矢量化的方法。
下面的代碼假設一個 NumPy 一維陣列作為輸入。
例如,如果以

(完整分析在這里)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/335418.html
上一篇:從不同的索引位置迭代串列中的元素
