Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 返回真值索引_Python_Loops_Numpy - Fatal编程技术网

Python 返回真值索引

Python 返回真值索引,python,loops,numpy,Python,Loops,Numpy,对于下面编写的代码,我想打印samples\u avg中的值索引,对于samples\u avg中的每个列表,它们在maxlist中返回true。那么名单呢 samples_avg = [[1, 12, 3], [15000, 4, 3], [1, 144, 45]] 我希望返回值像 filtered = [[], [0], [1, 2]] 因为第一个列表中没有条件为真的索引,所以第二个列表的索引0为真,最后一个列表中的索引1和2为真 samples_avg = [[1, 12, 3], [

对于下面编写的代码,我想打印
samples\u avg
中的值索引,对于
samples\u avg
中的每个列表,它们在
maxlist
中返回true。那么名单呢

samples_avg = [[1, 12, 3], [15000, 4, 3], [1, 144, 45]]
我希望返回值像

filtered = [[], [0], [1, 2]]
因为第一个列表中没有条件为真的索引,所以第二个列表的索引0为真,最后一个列表中的索引1和2为真

samples_avg = [[1, 12, 3], [15000, 4, 3], [1, 144, 45]]
def check(samples_avg):
    filtered = []
    maxval = [max(x) for x in zip(*samples_avg)]
    maxlist = ([r >= (m/5) for row in samples_avg for r, m in zip(row, maxval)])

    results =[[] for i in samples_avg]
    for i in results:
        filtered.append([x for x, y in zip(samples_avg[i], maxlist) if y == True])
        print (np.where((filtered) is True))

当前返回三个空数组。如何修复此问题,使数组包含索引?任何建议都将不胜感激

只是稍微修改了列表理解,使用enumerate获取实际索引,并在if子句中使用条件true,而不是映射值

试试这个:

samples_avg=[[1,12,3],[15000,4,3],[1,144,45]]
def检查(样本平均值):
maxval=[拉链中x的最大值(x)(*样本平均值)]
对于样本中的行,如果r>=(m/5)],则在枚举(zip(row,maxval))中返回[[i代表i,(r,m)]]
打印(检查(样本平均值))
输出:

[[], [0], [1, 2]]


实际上,我被
m/5
这件事搞糊涂了,但是nvm。使用
枚举
,就像@Adam.Er8中的答案一样