0

我有一个看起来有点像这样的 python 列表:

[[0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,90,1,9999,0,0,0,0,0,0,0,00,0],
[0,0,0,0,0,0,0,0,0,0,0,0,00,0],[0,0,90,1,9999,1,2,0,0,9999,0,0,00,0].....till about 30 rows]

我需要从该列表中找到具有 9999 的最大行,或者换句话说,它的所有元素都不为零。请帮我解决一下这个。谢谢!!

我试过了:

print max((numpy.where(v1==9999)[0])) 

但这只是给了我一些奇怪的错误,比如'int' object not iterablenumpy.where does not accept keywords n等等!

4

3 回答 3

2

你想要:

idx,row = max(enumerate(lst),key=lambda r: ( sum(r[1])==0, r[0] ) )

lst你的清单在哪里。

或者你想要:

next(x for x in reversed(lst) if sum(x) != 0)
于 2012-11-10T02:00:50.967 回答
0

这将返回具有非“全零”行的索引:

nonzerorows = [i for i,j in enumerate(a) if any(j)]

以及此类行的最高索引:

maxnonzerorows = max([i for i,j in enumerate(a) if any(j)])
于 2012-11-12T14:28:45.293 回答
0
    python 3.2

    #   if you want to find how many rows in your list has 9999
    v=[[0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,90,1,9999,0,0,0,0,0,0,0,00,0]]

    len([i for i in v if 9999 in i])




 #   if any element in your rows is 9999 then all the elements of that row 
 # cannot be 0. then if you want to know how many rows have all the elements 0.


     len([i for i in v if sum(i)==0])
于 2012-11-10T03:03:53.190 回答