Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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_Arrays_Numpy - Fatal编程技术网

Python 获取大于值的索引并保留值

Python 获取大于值的索引并保留值,python,arrays,numpy,Python,Arrays,Numpy,我有一个2D数组,看起来像这样: [[0.1, 0.2, 0.4, 0.6, 0.9] [0.3, 0.7, 0.8, 0.3, 0.9] [0.7, 0.9, 0.4, 0.6, 0.9] [0.1, 0.2, 0.6, 0.6, 0.9]] 我想保存数组大于0.6的索引,但我还想保留该位置的值,因此输出为: [0, 3, 0.6] [0, 4, 0.9] [1, 2, 0.7] 等等 为了获得索引,我做了以下操作: x = np.where(PPCF> 0.6) high_pc =

我有一个2D数组,看起来像这样:

[[0.1, 0.2, 0.4, 0.6, 0.9]
[0.3, 0.7, 0.8, 0.3, 0.9]
[0.7, 0.9, 0.4, 0.6, 0.9]
[0.1, 0.2, 0.6, 0.6, 0.9]]
我想保存数组大于0.6的索引,但我还想保留该位置的值,因此输出为:

[0, 3, 0.6]
[0, 4, 0.9]
[1, 2, 0.7]
等等

为了获得索引,我做了以下操作:

x = np.where(PPCF> 0.6)
high_pc = np.asarray(x).T.tolist()
但是如何将该值保持在第三个位置?

这应该可以:

x = np.where(PPCF> 0.6)
high_pc = np.asarray(x).T.tolist()
for i in high_pc:
    i.append(float(PPCF[i[0],i[1]]))
这应该起作用:

x = np.where(PPCF> 0.6)
high_pc = np.asarray(x).T.tolist()
for i in high_pc:
    i.append(float(PPCF[i[0],i[1]]))

您可以沿着列和行运行一个循环,检查每个元素是否大于阈值,并将它们保存在列表中

a = [[0.1, 0.2, 0.4, 0.6, 0.9],
[0.3, 0.7, 0.8, 0.3, 0.9],
[0.7, 0.9, 0.4, 0.6, 0.9],
[0.1, 0.2, 0.6, 0.6, 0.9]]

def find_ix(a, threshold = 0.6):
    res_list = []
    for i in range(len(a)):
        for j in range(len(a[i])):
            if a[i][j] >= threshold:
                res_list.append([i, j, a[i][j]])
    return res_list

print("Resulting list = \n ", find_ix(a))

您可以沿着列和行运行一个循环,检查每个元素是否大于阈值,并将它们保存在列表中

a = [[0.1, 0.2, 0.4, 0.6, 0.9],
[0.3, 0.7, 0.8, 0.3, 0.9],
[0.7, 0.9, 0.4, 0.6, 0.9],
[0.1, 0.2, 0.6, 0.6, 0.9]]

def find_ix(a, threshold = 0.6):
    res_list = []
    for i in range(len(a)):
        for j in range(len(a[i])):
            if a[i][j] >= threshold:
                res_list.append([i, j, a[i][j]])
    return res_list

print("Resulting list = \n ", find_ix(a))
简单,无循环:

x = np.where(PPCF > 0.6)                                                 # condition to screen values
vals = PPCF[x]                                                           # find values by indices
np.concatenate((np.array(x).T, vals.reshape(vals.size, 1)), axis = 1)    # resulting array
请随意将其转换为列表。

简单,无循环:

x = np.where(PPCF > 0.6)                                                 # condition to screen values
vals = PPCF[x]                                                           # find values by indices
np.concatenate((np.array(x).T, vals.reshape(vals.size, 1)), axis = 1)    # resulting array
import numpy as np

arr = np.array([[0.1, 0.2, 0.4, 0.6, 0.9],
    [0.3, 0.7, 0.8, 0.3, 0.9],
    [0.7, 0.9, 0.4, 0.6, 0.9],
    [0.1, 0.2, 0.6, 0.6, 0.9]]) 
rows, cols = np.where(arr > 0.6) # Get rows and columns where arr > 0.6
values = arr[rows, cols] # Get all values > 0.6 in arr
result = np.column_stack((rows, cols, values)) # Stack three columns to create final array
"""
 Result - 
 [ 0.   4.   0.9]
 [ 1.   1.   0.7]
 [ 1.   2.   0.8]
 [ 1.   4.   0.9]
 [ 2.   0.   0.7]
 [ 2.   1.   0.9]
 [ 2.   4.   0.9]
 [ 3.   4.   0.9]]
"""
请随意将其转换为列表

import numpy as np

arr = np.array([[0.1, 0.2, 0.4, 0.6, 0.9],
    [0.3, 0.7, 0.8, 0.3, 0.9],
    [0.7, 0.9, 0.4, 0.6, 0.9],
    [0.1, 0.2, 0.6, 0.6, 0.9]]) 
rows, cols = np.where(arr > 0.6) # Get rows and columns where arr > 0.6
values = arr[rows, cols] # Get all values > 0.6 in arr
result = np.column_stack((rows, cols, values)) # Stack three columns to create final array
"""
 Result - 
 [ 0.   4.   0.9]
 [ 1.   1.   0.7]
 [ 1.   2.   0.8]
 [ 1.   4.   0.9]
 [ 2.   0.   0.7]
 [ 2.   1.   0.9]
 [ 2.   4.   0.9]
 [ 3.   4.   0.9]]
"""
您可以将结果转换为列表


您可以将结果转换为列表

我喜欢你的解决方案!谢谢你:喜欢你的解决方案!谢谢您: