努比的新朋友。我有一个由1和0组成的二维数组,我正试图对角扫描一个特定长度的连续1。找到模式后,函数应返回模式开始的索引,即拉伸中第一个“1”的位置。 这是我最好的尝试:
def find_pattern(array2D, patternlength):
ones_count = 0
pattern_location = []
diag = [array2D.diagonal(i) for i in range(array2D.shape[1]-1,-array2D.shape[0],-1)]
for index, match in np.ndenumerate(diag):
if match == 1:
ones_count +=1
else:
ones_count == 0
if ones_count == patternlength:
pattern_location.append(index)
return pattern_location
但是,尝试运行时会产生ValueError:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我理解为什么会出现这个错误,但我不知道如何处理它。any()或all()似乎不适用于我正在寻找某个连续序列的实例
我正在寻找一种解决方案,它不需要使用额外的软件包,如pandas和itertools
谢谢
我觉得你太复杂了,不如:
样本输入:
返回:
相关问题 更多 >
编程相关推荐