[英]How to find index of the window which has certain number of values greater than threshold?
[英]How do I figure out the value which is greater than certain threshold in a matrix?
假設我有一個矩陣:
a = [[4,7,2],[0,1,4],[4,5,6]]
我想得到
b = [0, 1]
c = [[2],[0,1]]
b = [0,1]
因為位置0
和1
處a
的內部列表包含小於3
值。c = [[2],[0,1]]
因為b
第一個子列表的[2]
nd 元素小於 3 和[0,1]
因為b
第二個子列表中的第一個和第二個元素小於 3 .我試過 :
for i in a:
for o in i:
if o < 3:
print(i)
它只返回原始矩陣。
我如何在python中獲得b
& c
?
您可以利用enumerate(iterable[,startingvalue])
它為您提供enumerate(iterable[,startingvalue])
的索引和值:
a = [[4,7,2],[0,1,4],[4,5,6]]
thresh = 3
b = [] # collects indexes of inner lists with values smaller then thresh
c = [] # collects indexes in the inner lists that are smaller then thresh
for idx, inner_list in enumerate(a):
if any(value < thresh for value in inner_list):
b.append(idx)
c.append([])
for idx_2, value in enumerate(inner_list):
if value < thresh:
c[-1].append(idx_2)
print(a)
print(b)
print(c)
輸出:
[[4, 7, 2], [0, 1, 4], [4, 5, 6]]
[0, 1]
[[2], [0, 1]]
獨行:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.