I'm trying to get the index of elements in a row that is filled.
我正在尝试获取已填充的行中的元素索引。
I tried to use numpy where() function, but it's only returning the index of non-zero elements.
我试图使用numpy where()函数,但它只返回非零元素的索引。
import numpy as np
board = np.array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[2, 2, 2, 2],
[0, 0, 0, 0]])
for rows in board:
if set(rows) == {2}:
if len(set(rows)) <= 1:
print(np.where(board == rows))
My desired output is the following:
我想要的输出如下:
(array([9, 9, 9, 9], dtype=int32), array([0, 1, 2, 3], dtype=int32))
that is, row, col.
就是,行,col。
However, I'm getting this instead:
但是,我得到了这个:
(array([2, 9, 9, 9, 9], dtype=int32), array([1, 0, 1, 2, 3], dtype=int32))
As stated above, it should only get the index of elements in a filled row. The board1 is not filled with 2's, yet it's being included.
如上所述,它应该只获得填充行中元素的索引。 board1没有填充2,但它被包含在内。
3 个解决方案
#1
1
You don't need loops for this at all. For finding the rows that are filled with something other than 0, use .all(axis=1)
. The axis
argument tells which axis to look for matching values across:
你根本不需要循环。要查找用0以外的其他内容填充的行,请使用.all(axis = 1)。 axis参数告诉哪个轴在以下位置查找匹配值:
>>> (board != 0).all(axis=1)
array([False, False, False, False, False, False, False, False, False,
True, False], dtype=bool)
The booleans are True
wherever there is a row filled with non-zero values, and False
otherwise. If you take the outer
product with a row array full of True
values, you'll get True
exactly in your matrix locations corresponding to the full row:
如果有一行填充非零值,则布尔值为True,否则为False。如果您使用一个充满True值的行数组来获取外部产品,那么您将在与整行对应的矩阵位置中获得True:
>>> np.outer((board!=0).all(axis=1),np.ones(board.shape[1], dtype=bool))
array([[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[ True, True, True, True],
[False, False, False, False]], dtype=bool)
Then you can get the indices of those True
values with np.where()
:
然后,您可以使用np.where()获取这些True值的索引:
>>> np.where(np.outer((board!=0).all(axis=1),np.ones(board.shape[1], dtype=bool)))
(array([9, 9, 9, 9]), array([0, 1, 2, 3]))
Note that np.where()
returns np.int64
values. If you want np.int32
then just assign variables and convert them:
请注意,np.where()返回np.int64值。如果你想要np.int32,那么只需分配变量并转换它们:
>>> yind, xind = np.where(np.outer((board!=0).all(axis=1),np.ones(board.shape[1], dtype=bool)))
>>> yind = np.int32(yind)
>>> xind = np.int32(xind)
>>> yind, xind
(array([9, 9, 9, 9], dtype=int32), array([0, 1, 2, 3], dtype=int32))
To instead put all of these indices as tuples, use zip(*)
(see here for an explanation if you're not familiar with *
unpacking):
要将所有这些索引作为元组,请使用zip(*)(如果您不熟悉*解包,请参阅此处获取解释):
>>> [(y,x) for y,x in zip(*np.where(np.outer((board!=0).all(1), np.ones(board.shape[1]))))]
[(9, 0), (9, 1), (9, 2), (9, 3)]
And again if you need np.int32
just specify that in the comprehension:
如果你需要np.int32,请再次在理解中指定:
>>> [(y.astype(np.int32),x.astype(np.int32)) for y,x in zip(*np.where(np.outer((board == 2).all(1), np.ones(board.shape[1]))))]
[(9, 0), (9, 1), (9, 2), (9, 3)]
#2
0
If i understood well you are trying to find the row where your numpy array is completely filled ( no 0 values )
如果我理解你正在尝试找到你的numpy数组完全填充的行(没有0值)
so i ask of you to consider this:
所以我请你考虑一下:
import numpy as np
board = np.array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 2, 0, 0],
[1, 1, 1, 1], # added this one just to generalize in
[0, 0, 0, 0], # case you have numbers != 2
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[2, 2, 2, 2],
[0, 0, 0, 0]])
for i,rows in enumerate(board):
if not 0 in set(rows): # changed this to work on all non zero numbers
print(rows)
if len(set(rows)) <= 1:
print(i)
output:
[1 1 1 1] # first row filled
3 # its index as int ( if that's what you're looking for )
[2 2 2 2] # seconds row filled
9 # its index as int
all you needed is enumerate built-in function. Happy Coding
你需要的只是枚举内置函数。快乐的编码
#3
0
If I understood your problem correctly, you want to find the row index of a specific element in the array. So, you need the following print()
statement instead of print(np.where(board == rows))
.
如果我正确理解了您的问题,您希望找到数组中特定元素的行索引。因此,您需要以下print()语句而不是print(np.where(board == rows))。
print(np.where((board == rows).all(axis=1)))
It outputs:
(array([9], dtype=int64),)
To get the specific row number, you can do the following.
要获取特定的行号,您可以执行以下操作。
loc = np.where((board == rows).all(axis=1))
print(loc[0][0]) # prints 9
Complete program:
import numpy as np
board = np.array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[2, 2, 2, 2],
[0, 0, 0, 0]])
for rows in board:
if set(rows) == {2}:
print(rows)
if len(set(rows)) <= 1:
print(np.where((board == rows).all(axis=1)))
#1
1
You don't need loops for this at all. For finding the rows that are filled with something other than 0, use .all(axis=1)
. The axis
argument tells which axis to look for matching values across:
你根本不需要循环。要查找用0以外的其他内容填充的行,请使用.all(axis = 1)。 axis参数告诉哪个轴在以下位置查找匹配值:
>>> (board != 0).all(axis=1)
array([False, False, False, False, False, False, False, False, False,
True, False], dtype=bool)
The booleans are True
wherever there is a row filled with non-zero values, and False
otherwise. If you take the outer
product with a row array full of True
values, you'll get True
exactly in your matrix locations corresponding to the full row:
如果有一行填充非零值,则布尔值为True,否则为False。如果您使用一个充满True值的行数组来获取外部产品,那么您将在与整行对应的矩阵位置中获得True:
>>> np.outer((board!=0).all(axis=1),np.ones(board.shape[1], dtype=bool))
array([[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[False, False, False, False],
[ True, True, True, True],
[False, False, False, False]], dtype=bool)
Then you can get the indices of those True
values with np.where()
:
然后,您可以使用np.where()获取这些True值的索引:
>>> np.where(np.outer((board!=0).all(axis=1),np.ones(board.shape[1], dtype=bool)))
(array([9, 9, 9, 9]), array([0, 1, 2, 3]))
Note that np.where()
returns np.int64
values. If you want np.int32
then just assign variables and convert them:
请注意,np.where()返回np.int64值。如果你想要np.int32,那么只需分配变量并转换它们:
>>> yind, xind = np.where(np.outer((board!=0).all(axis=1),np.ones(board.shape[1], dtype=bool)))
>>> yind = np.int32(yind)
>>> xind = np.int32(xind)
>>> yind, xind
(array([9, 9, 9, 9], dtype=int32), array([0, 1, 2, 3], dtype=int32))
To instead put all of these indices as tuples, use zip(*)
(see here for an explanation if you're not familiar with *
unpacking):
要将所有这些索引作为元组,请使用zip(*)(如果您不熟悉*解包,请参阅此处获取解释):
>>> [(y,x) for y,x in zip(*np.where(np.outer((board!=0).all(1), np.ones(board.shape[1]))))]
[(9, 0), (9, 1), (9, 2), (9, 3)]
And again if you need np.int32
just specify that in the comprehension:
如果你需要np.int32,请再次在理解中指定:
>>> [(y.astype(np.int32),x.astype(np.int32)) for y,x in zip(*np.where(np.outer((board == 2).all(1), np.ones(board.shape[1]))))]
[(9, 0), (9, 1), (9, 2), (9, 3)]
#2
0
If i understood well you are trying to find the row where your numpy array is completely filled ( no 0 values )
如果我理解你正在尝试找到你的numpy数组完全填充的行(没有0值)
so i ask of you to consider this:
所以我请你考虑一下:
import numpy as np
board = np.array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 2, 0, 0],
[1, 1, 1, 1], # added this one just to generalize in
[0, 0, 0, 0], # case you have numbers != 2
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[2, 2, 2, 2],
[0, 0, 0, 0]])
for i,rows in enumerate(board):
if not 0 in set(rows): # changed this to work on all non zero numbers
print(rows)
if len(set(rows)) <= 1:
print(i)
output:
[1 1 1 1] # first row filled
3 # its index as int ( if that's what you're looking for )
[2 2 2 2] # seconds row filled
9 # its index as int
all you needed is enumerate built-in function. Happy Coding
你需要的只是枚举内置函数。快乐的编码
#3
0
If I understood your problem correctly, you want to find the row index of a specific element in the array. So, you need the following print()
statement instead of print(np.where(board == rows))
.
如果我正确理解了您的问题,您希望找到数组中特定元素的行索引。因此,您需要以下print()语句而不是print(np.where(board == rows))。
print(np.where((board == rows).all(axis=1)))
It outputs:
(array([9], dtype=int64),)
To get the specific row number, you can do the following.
要获取特定的行号,您可以执行以下操作。
loc = np.where((board == rows).all(axis=1))
print(loc[0][0]) # prints 9
Complete program:
import numpy as np
board = np.array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[2, 2, 2, 2],
[0, 0, 0, 0]])
for rows in board:
if set(rows) == {2}:
print(rows)
if len(set(rows)) <= 1:
print(np.where((board == rows).all(axis=1)))