英文:
Identifying ones in each row and creating a list in Python
问题
我有一个数组A
。我正在识别每一行中除了行号本身以外的位置上的1,并创建一个列表。例如,在A[0]
中,1应该在位置2,3,5
被识别,而不是0
。我展示了当前和期望的输出。
import numpy as np
A=np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1]])
output = []
for i, row in enumerate(A):
ones_indices = np.where(row == 1)[0]
other_rows = np.arange(A.shape[0])
other_rows = np.delete(other_rows, i)
output.append([[i], other_rows.tolist()])
print(output)
当前输出是
[[[0], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]], [[1], [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]], [[2], [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11]], [[3], [0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11]], [[4], [0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11]], [[5], [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11]], [[6], [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11]], [[7], [0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11]], [[8], [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11]], [[9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11]], [[10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]], [[11], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]]
期望输出是
[[[0], [2,3,5]], [[1], [3,4,6]], [[2], [0,3,5]], [[3], [0, 1, 2, 4, 5, 6]], [[4], [1,3,6]], [[5], [0,2,3,7,8,10]], [[6], [1,3,4,8,9,11]], [[7], [5,8,10]], [[8], [5,6,7,9,10,11]], [[9], [6,8,11]], [[10], [5,7,8], [[11], [6,8,9]]]
英文:
I have an array A
. I am identifying ones in each row except the row number itself and creating a list. For example, in A[0]
, the ones should be identified for locations 2,3,5
and not 0
. I present the current and expected output.
import numpy as np
A=np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1]])
output = []
for i, row in enumerate(A):
ones_indices = np.where(row == 1)[0]
other_rows = np.arange(A.shape[0])
other_rows = np.delete(other_rows, i)
output.append([[i], other_rows.tolist()])
print(output)
The current output is
[[[0], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]], [[1], [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]], [[2], [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11]], [[3], [0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11]], [[4], [0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11]], [[5], [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11]], [[6], [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11]], [[7], [0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11]], [[8], [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11]], [[9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11]], [[10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]], [[11], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]]
The expected output is
[[[0], [2,3,5]], [[1], [3,4,6]], [[2], [0,3,5]], [[3], [0, 1, 2, 4, 5, 6]], [[4], [1,3,6]], [[5], [0,2,3,7,8,10]], [[6], [1,3,4,8,9,11]], [[7], [5,8,10]], [[8], [5,6,7,9,10,11]], [[9], [6,8,11]], [[10], [5,7,8], [[11], [6,8,9]]]
答案1
得分: 4
以下是翻译的内容:
使用NumPy的方法是 fill_diagonal
,然后使用 where
:
np.fill_diagonal(A, 0)
row, idx = np.where(A==1) # 如果只有0/1,可以使用 np.where(A)
输出:
(array([ 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4,
4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8,
8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11]),
array([ 2, 3, 5, 3, 4, 6, 0, 3, 5, 0, 1, 2, 4, 5, 6, 1, 3,
6, 0, 2, 3, 7, 8, 10, 1, 3, 4, 8, 9, 11, 5, 8, 10, 5,
6, 7, 9, 10, 11, 6, 8, 11, 5, 7, 8, 6, 8, 9]))
如果你真的想要嵌套列表:
np.fill_diagonal(A, 0)
out = [[[i], np.where(a==1)[0].tolist()] for i, a in enumerate(A)]
或者:
row, idx = np.where(A==1)
x = np.array_split(idx, np.where(row[1:]!=row[:-1])[0]+1)
out = [[[i], j.tolist()] for i,j in enumerate(x)]
输出:
[[[0], [2, 3, 5]],
[[1], [3, 4, 6]],
[[2], [0, 3, 5]],
[[3], [0, 1, 2, 4, 5, 6]],
[[4], [1, 3, 6]],
[[5], [0, 2, 3, 7, 8, 10]],
[[6], [1, 3, 4, 8, 9, 11]],
[[7], [5, 8, 10]],
[[8], [5, 6, 7, 9, 10, 11]],
[[9], [6, 8, 11]],
[[10], [5, 7, 8]],
[[11], [6, 8, 9]]]
英文:
The numpy approach would be to fill_diagonal
, then to use where
:
np.fill_diagonal(A, 0)
row, idx = np.where(A==1) # np.where(A) if only 0/1
Output:
(array([ 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4,
4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8,
8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11]),
array([ 2, 3, 5, 3, 4, 6, 0, 3, 5, 0, 1, 2, 4, 5, 6, 1, 3,
6, 0, 2, 3, 7, 8, 10, 1, 3, 4, 8, 9, 11, 5, 8, 10, 5,
6, 7, 9, 10, 11, 6, 8, 11, 5, 7, 8, 6, 8, 9]))
If you really want nested lists:
np.fill_diagonal(A, 0)
out = [[[i], np.where(a==1)[0].tolist()] for i, a in enumerate(A)]
Or:
row, idx = np.where(A==1)
x = np.array_split(idx, np.where(row[1:]!=row[:-1])[0]+1)
out = [[[i], j.tolist()] for i,j in enumerate(x)]
Output:
[[[0], [2, 3, 5]],
[[1], [3, 4, 6]],
[[2], [0, 3, 5]],
[[3], [0, 1, 2, 4, 5, 6]],
[[4], [1, 3, 6]],
[[5], [0, 2, 3, 7, 8, 10]],
[[6], [1, 3, 4, 8, 9, 11]],
[[7], [5, 8, 10]],
[[8], [5, 6, 7, 9, 10, 11]],
[[9], [6, 8, 11]],
[[10], [5, 7, 8]],
[[11], [6, 8, 9]]]
答案2
得分: 3
这可能是一种非常复杂的方法,但它有效。
- 用零填充矩阵A的对角线,因为你不想要1所在的行的索引。
- 使用
np.where
获取1的位置。 - 使用 这个答案 按行分组。
- 使用列表推导将行号和1的位置合并。
import numpy as np
A = np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1]])
# 步骤 1
np.fill_diagonal(A, 0)
# 步骤 2
a = np.vstack(np.where(A == 1)).T
# 步骤 3
k = np.split(a[:, 1], np.unique(a[:, 0], return_index=True)[1][1:])
# 步骤 4
res = [[[i], _k.tolist()] for i, _k in enumerate(k)]
通过这样做,我们得到 res
:
[[[0], [2, 3, 5]], [[1], [3, 4, 6]], [[2], [0, 3, 5]], [[3], [0, 1, 2, 4, 5, 6]], [[4], [1, 3, 6]], [[5], [0, 2, 3, 7, 8, 10]], [[6], [1, 3, 4, 8, 9, 11]], [[7], [5, 8, 10]], [[8], [5, 6, 7, 9, 10, 11]], [[9], [6, 8, 11]], [[10], [5, 7, 8]], [[11], [6, 8, 9]]]
英文:
This is possibly a very convoluted way of doing this, but it works.
- Fill the diagonal of A with zeros, since you don't want the index of the row the 1 is in.
- Use
np.where
to get the locations of the 1s. - Use this answer to group by row.
- Use list comprehension to combine the row number and the locations of the 1s.
import numpy as np
A = np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1]])
# step 1
np.fill_diagonal(A, 0)
# step 2
a = np.vstack(np.where(A == 1)).T
# step 3
k = np.split(a[:, 1], np.unique(a[:, 0], return_index=True)[1][1:])
# step 4
res = [[[i], _k.tolist()] for i, _k in enumerate(k)]
With this, we have res
:
[[[0], [2, 3, 5]], [[1], [3, 4, 6]], [[2], [0, 3, 5]], [[3], [0, 1, 2, 4, 5, 6]], [[4], [1, 3, 6]], [[5], [0, 2, 3, 7, 8, 10]], [[6], [1, 3, 4, 8, 9, 11]], [[7], [5, 8, 10]], [[8], [5, 6, 7, 9, 10, 11]], [[9], [6, 8, 11]], [[10], [5, 7, 8]], [[11], [6, 8, 9]]]
答案3
得分: 2
这是另一种方法。我还没有检查过时间,但它给出了预期的结果。
import numpy as np
A = np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1]])
# 用0替换对角线上的元素
np.fill_diagonal(A, 0)
# 创建一个1D数组
b = np.arange(A.shape[0])
# 使用广播进行元素逐个相乘
c = np.multiply(A, b)
# 获取非零索引
rw, cl = c.nonzero()
# 获取[row,column]坐标
a = np.c_[rw, cl]
# 按行分组并拆分
val = np.split(a[:, 1], np.unique(a[:, 0], return_index=True)[1][1:])
# 列表推导式
out = [[[i], ar.tolist()] for i, ar in enumerate(val)]
print(out)
输出结果为:
[[[0], [2, 3, 5]], [[1], [3, 4, 6]], [[2], [0, 3, 5]], [[3], [0, 1, 2, 4, 5, 6]], [[4], [1, 3, 6]], [[5], [0, 2, 3, 7, 8, 10]], [[6], [1, 3, 4, 8, 9, 11]], [[7], [5, 8, 10]], [[8], [5, 6, 7, 9, 10, 11]], [[9], [6, 8, 11]], [[10], [5, 7, 8]], [[11], [6, 8, 9]]]
英文:
Here is another way. I haven't checked the timings. Yet it gives the expected results.
import numpy as np
A=np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1]])
# replace row indices with 0
np.fill_diagonal(A,0)
# create a 1D array
b = np.arange(A.shape[0])
# multiply element wise with broadcasting
c = np.multiply(A,b)
# get nonzero indices
rw, cl = c.nonzero()
# get [row,column] coordinates
a = np.c_[rw,cl]
# groupby row and split
val = np.split(a[:,1], np.unique(a[:, 0], return_index=True)[1][1:])
# list comprehension
out = [[[i],ar.tolist()] for i,ar in enumerate(val)]
print(out)
>> [[[0], [2, 3, 5]], [[1], [3, 4, 6]], [[2], [0, 3, 5]], [[3], [0, 1, 2, 4, 5, 6]], [[4], [1, 3, 6]], [[5], [0, 2, 3, 7, 8, 10]], [[6], [1, 3, 4, 8, 9, 11]], [[7], [5, 8, 10]], [[8], [5, 6, 7, 9, 10, 11]], [[9], [6, 8, 11]], [[10], [5, 7, 8]], [[11], [6, 8, 9]]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论