Python 获取列表中满足条件的元素的索引

Python 获取列表中满足条件的元素的索引,python,list,Python,List,我正在尝试对整数列表的位置进行索引,以获得大于0的整数的位置 这是我的清单: paying=[0,0,0,1,0,3,4,0,5] 这是期望的输出: [3,5,6,8] rdo=paying[paying>0] 并尝试: rdo=paying.index(paying>0) 两种情况下的输出都是相同的 typeerror > not suported between instances of list and int 使用和内置功能: paying=[0,0,0,1,

我正在尝试对整数列表的位置进行索引,以获得大于0的整数的位置

这是我的清单:

paying=[0,0,0,1,0,3,4,0,5]
这是期望的输出:

[3,5,6,8]

rdo=paying[paying>0]
并尝试:

rdo=paying.index(paying>0)
两种情况下的输出都是相同的

typeerror > not suported between instances of list and int
使用和内置功能:

paying=[0,0,0,1,0,3,4,0,5]
print([i for i, e in enumerate(paying) if e > 0])
[3,5,6,8]


您可以使用列表理解

paying=[0,0,0,1,0,3,4,0,5]
result = [index for index, value in enumerate(paying) if value > 0]
使用枚举:

paying=[0,0,0,1,0,3,4,0,5]
[i for i, e in enumerate(paying) if e > 0]


paying[paying>0]
是numpy语法
[paying.index(e) for e in paying if e > 0]

Result:  [3, 5, 6, 8]