简体   繁体   English

Python:如何在每一行中找到满足条件的元素的索引,并将它们转换为字典?

[英]Python: How to find indices of elements that satisfy conditions in each row, and transformed them to a dict?

An example:一个例子:

import numpy as np
np.random.seed(20211021)
myarray = np.random.randint(0, 5, size=(5, 4))

>>> myarray
array([[2, 3, 0, 1],
       [3, 3, 3, 1],
       [1, 0, 0, 0],
       [3, 2, 4, 0],
       [4, 1, 4, 0]])

Here I use argwhere in numpy to find indices of elements that greater than 0 in each row.在这里,我在numpy使用argwhere来查找每行中大于 0 的元素的索引。

g0 = np.argwhere(myarray > 0)
>>> g0
array([[0, 0],
       [0, 1],
       [0, 3],
       [1, 0],
       [1, 1],
       [1, 2],
       [1, 3],
       [2, 0],
       [3, 0],
       [3, 1],
       [3, 2],
       [4, 0],
       [4, 1],
       [4, 2]], dtype=int64)

The dices g0 is a two-dimension array.骰子g0是一个二维数组。 The form of indices that I intend to create is like below:我打算创建的索引形式如下:

{
    0: [0, 1, 3],
    1: [0, 1, 2, 3],
    2: [0],
    3: [0, 1, 2],
    4: [0, 1, 2]
}

Is there any way in which g0 can be transformed to a dict?有什么方法可以将g0转换为 dict 吗? (Other than applying function to each row of myarray I hasn't find an efficient method) (除了将函数应用于myarray每一行,我还没有找到有效的方法)

np.unique can be used with indexes to get both the dictionary keys and locations, then use np.split to divide the array, then zip together the keys and the arrays to build the dictionary from the tuples: np.unique可以与索引一起使用以获取字典键和位置,然后使用np.split分割数组,然后将键和数组zip在一起以从元组构建字典:

g0 = np.argwhere(myarray > 0)
keys, locs = np.unique(g0[:, 0], return_index=True)
d = dict(zip(keys, np.split(g0[:, 1], locs[1:])))

np.nonzero may be faster than np.argwhere in this case:在这种情况下, np.nonzero可能比np.argwhere更快:

i, v = np.nonzero(myarray > 0)
keys, locs = np.unique(i, return_index=True)
d = dict(zip(keys, np.split(v, locs[1:])))

However, a simple dictionary comprehension is likely the fastest option on smaller arrays:但是,简单的字典理解可能是较小数组上最快的选择:

d = {i: np.nonzero(r > 0)[0] for i, r in enumerate(myarray)}

All options produce d :所有选项都会产生d

{0: array([0, 1, 3]),
 1: array([0, 1, 2, 3]),
 2: array([0]),
 3: array([0, 1, 2]),
 4: array([0, 1, 2])}

Setup and imports:设置和导入:

import numpy as np

np.random.seed(20211021)
myarray = np.random.randint(0, 5, size=(5, 4))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM