从布尔数组中提取连续的 True 值,同时保留索引
Extracting consecutive True values from Boolean array, while keeping indices
有个问题一直困扰着我。我有一个布尔数组 (n=1320),我想在其中提取至少 6 个连续真值的序列并保留原始索引。
例如:
Input: [True, True, True, True, True, True, False, False, False, False, False, True , True , False, False, False]
Output: [True, True, True, True, True, True, False, False, False, False, False, False, False, False, False, False]
提前致谢(在 Python 3.8 中与 Jupyter Lab 一起工作)
/约翰
你的测试用例有几个问题。首先,您的测试用例没有 6 个连续的 True
。但是返回一个输出。
以下是您可能会觉得有用的代码。请注意,我更改了 2 个数组,使它们包含 6 个连续的 True
。您可能想删除 Array1 和 Array2 中的最后 6 项以查看它是否有效。
如果我错误地理解了你的问题,请告诉它。你的问题有点不清楚。
祝你好运。
Array1 = [True, True, False, False, False, True, True, False, False, False, False, True, True, False, False, False, True, True, True, True, True, True]
Array2 = [True, True, False, False, False, False, True, True, False, False, False, False, False, True, True, False, True, True, True, True, True, True]
isConsecutive = False
for i in range(len(Array1)):
try:
if Array1[i] and Array1[i+1] and Array1[i+2] and Array1[i+3] and Array1[i+4] and Array1[i+5]:
isConsecutive = True
break
except:
pass
try:
if Array2[i] and Array2[i+1] and Array2[i+2] and Array2[i+3] and Array2[i+4] and Array2[i+5]:
isConsecutive = True
break
except:
pass
Output = []
if isConsecutive:
for i in range(len(Array1)):
if Array1[i] or Array2[i]:
Output.append(True)
else:
Output.append(False)
print(Output)
else:
print("Your arrays don't have six or more consecutive 'True's!")
输出:
[True, True, False, False, False, True, True, True, False, False, False, True, True, True, True, False, True, True, True, True, True, True]
我认为这可以解决问题:
arr1 = [True, True, True, True, True, True, False, False, False, False, False, True , True , False, False, False]
def check_for_six(arr):
arr = arr.copy() # avoid mutating the original list
counting = [] # keep track of True indexes, to count them later
for i in range(len(arr)): # cycle by index
is_last = i + 1 >= len(arr) # True if this is the last index in the array
if arr[i] == True:
counting.append(i) # add value to list if True
if is_last or arr[i] == False: # when we are at the last entry, or find a False
if len(counting) < 6: # check the length of our True indexes, and if less than 6
for j in counting:
arr[j] = False # make each False
counting = []
return arr
print( check_for_six(arr1) )
输出:
[True, True, True, True, True, True, False, False, False, False, False, False, False, False, False, False, False]
有个问题一直困扰着我。我有一个布尔数组 (n=1320),我想在其中提取至少 6 个连续真值的序列并保留原始索引。
例如:
Input: [True, True, True, True, True, True, False, False, False, False, False, True , True , False, False, False]
Output: [True, True, True, True, True, True, False, False, False, False, False, False, False, False, False, False]
提前致谢(在 Python 3.8 中与 Jupyter Lab 一起工作)
/约翰
你的测试用例有几个问题。首先,您的测试用例没有 6 个连续的 True
。但是返回一个输出。
以下是您可能会觉得有用的代码。请注意,我更改了 2 个数组,使它们包含 6 个连续的 True
。您可能想删除 Array1 和 Array2 中的最后 6 项以查看它是否有效。
如果我错误地理解了你的问题,请告诉它。你的问题有点不清楚。
祝你好运。
Array1 = [True, True, False, False, False, True, True, False, False, False, False, True, True, False, False, False, True, True, True, True, True, True]
Array2 = [True, True, False, False, False, False, True, True, False, False, False, False, False, True, True, False, True, True, True, True, True, True]
isConsecutive = False
for i in range(len(Array1)):
try:
if Array1[i] and Array1[i+1] and Array1[i+2] and Array1[i+3] and Array1[i+4] and Array1[i+5]:
isConsecutive = True
break
except:
pass
try:
if Array2[i] and Array2[i+1] and Array2[i+2] and Array2[i+3] and Array2[i+4] and Array2[i+5]:
isConsecutive = True
break
except:
pass
Output = []
if isConsecutive:
for i in range(len(Array1)):
if Array1[i] or Array2[i]:
Output.append(True)
else:
Output.append(False)
print(Output)
else:
print("Your arrays don't have six or more consecutive 'True's!")
输出:
[True, True, False, False, False, True, True, True, False, False, False, True, True, True, True, False, True, True, True, True, True, True]
我认为这可以解决问题:
arr1 = [True, True, True, True, True, True, False, False, False, False, False, True , True , False, False, False]
def check_for_six(arr):
arr = arr.copy() # avoid mutating the original list
counting = [] # keep track of True indexes, to count them later
for i in range(len(arr)): # cycle by index
is_last = i + 1 >= len(arr) # True if this is the last index in the array
if arr[i] == True:
counting.append(i) # add value to list if True
if is_last or arr[i] == False: # when we are at the last entry, or find a False
if len(counting) < 6: # check the length of our True indexes, and if less than 6
for j in counting:
arr[j] = False # make each False
counting = []
return arr
print( check_for_six(arr1) )
输出:
[True, True, True, True, True, True, False, False, False, False, False, False, False, False, False, False, False]