根据索引从矩阵中删除一些元素
remove some elements from a matrix according to the indices
我有一个矩阵:
a = ([[1, 0, 0, 0],
[0, 0, 1, 1],
[0, 1, 0, 1]
[1, 0, 0, 1]])
我想打印矩阵中的 0 但不是所有的 0。我只想保留索引最小的每一行中的 0,并删除该行中所有后续的零。
例如,在该矩阵的第一行中,应保留第二个元素(a[0][1]
),并应删除第一行中的其余元素,因为它们都是零。
我将 pop()
用于二维数组,但出现属性错误。而且输出也不正确。我不知道如何比较索引和 select 每行中的最小列索引。
这是我的代码:
for ix, row in enumerate(a):
for iy, i in enumerate(row):
if i==0 and (iy+ix<(iy+1)+ix) :
a[ix].pop((iy+1))
print(ix,iy)
elif i==0 and (iy+ix>(iy+1)+ix):
a[ix].pop(iy)
print(ix,iy+1)
print(a)
我的预期结果是一组索引和修改后的矩阵a。
0 1
1 0
2 0
3 1
a=[[1,0],[0,1,1],[0,1,1],[1,0]]
谁能帮帮我?
试试这个:
a = [[1, 0, 0, 0],
[0, 0, 1, 1],
[0, 1, 0, 1],
[1, 0, 0, 1]]
b = []
for i in a:
f = False
c = []
for j in i:
if (j==0 and f==False) or j != 0:
c.append(j)
if j == 0: f = True
else:
continue
b.append(c)
输出:
[[1, 0], [0, 1, 1], [0, 1, 1], [1, 0, 1]]
要获取数组中的索引 zero
,您可以试试这个:
list({i : j.index(0) for i,j in enumerate(b)}.items())
# [(0, 1), (1, 0), (2, 0), (3, 1)]
查看您的示例输入
a = [[1,0,0,0],[0,0,1,1],[0,1,0,1],[1,0,0,1]]
和预期的输出
>>[(0, 1), (1, 0), (2, 0), (3, 1)]
您可以将问题重新定义为在每行中查找值为零的元素的索引(如果存在多个元素,return 第一个)。
通过这种方式构建,解决方案就像遍历 a
的每一行并检索值 0
的 index
一样简单(因此只有第一个元素会默认为 return。
使用如下所示的列表理解:
value_to_find = 0
desired_indexes = [
row.index(value_to_find) for row in a
]
或使用 map
将是:
value_to_find = 0
desired_indexes = map(lambda row:row.index(value_to_find), a)
然后您可以枚举它们以将结果与行号配对
enumerate(desired_indexes)
瞧瞧!
>>[(0, 1), (1, 0), (2, 0), (3, 1)]
整个解决方案可以像这样写在一行中:
answer = list(enumerate(map(lambda row:row.index(0), a)))
假设每一行都有一个零,你可以用
得到它的列索引
c = np.argmin(a, axis=1)
或者,如果矩阵可以包含负数,你可以这样做
c = np.argmax(np.equal(a, 0), axis=1)
行正好
r = np.arange(len(a))
你想要的结果就是
result = np.stack((r, c), axis=-1)
如果有行中没有零,您可以使用掩码过滤结果:
mask = np.array(a)[r, c] == 0
result = result[mask, :]
此解决方案仅在每一行中至少有一个零时才有效。
indices = []
for x,row in enumerate(a):
i = row.index(0)
indices.append((x,i))
a[x] = row[:i+1] + [e for e in row[i:] if e]
print(indices)
print(a)
输出
[(0, 1), (1, 0), (2, 0), (3, 1)]
[[1, 0], [0, 1, 1], [0, 1, 1], [1, 0, 1]]
我有一个矩阵:
a = ([[1, 0, 0, 0],
[0, 0, 1, 1],
[0, 1, 0, 1]
[1, 0, 0, 1]])
我想打印矩阵中的 0 但不是所有的 0。我只想保留索引最小的每一行中的 0,并删除该行中所有后续的零。
例如,在该矩阵的第一行中,应保留第二个元素(a[0][1]
),并应删除第一行中的其余元素,因为它们都是零。
我将 pop()
用于二维数组,但出现属性错误。而且输出也不正确。我不知道如何比较索引和 select 每行中的最小列索引。
这是我的代码:
for ix, row in enumerate(a):
for iy, i in enumerate(row):
if i==0 and (iy+ix<(iy+1)+ix) :
a[ix].pop((iy+1))
print(ix,iy)
elif i==0 and (iy+ix>(iy+1)+ix):
a[ix].pop(iy)
print(ix,iy+1)
print(a)
我的预期结果是一组索引和修改后的矩阵a。
0 1
1 0
2 0
3 1
a=[[1,0],[0,1,1],[0,1,1],[1,0]]
谁能帮帮我?
试试这个:
a = [[1, 0, 0, 0],
[0, 0, 1, 1],
[0, 1, 0, 1],
[1, 0, 0, 1]]
b = []
for i in a:
f = False
c = []
for j in i:
if (j==0 and f==False) or j != 0:
c.append(j)
if j == 0: f = True
else:
continue
b.append(c)
输出:
[[1, 0], [0, 1, 1], [0, 1, 1], [1, 0, 1]]
要获取数组中的索引 zero
,您可以试试这个:
list({i : j.index(0) for i,j in enumerate(b)}.items())
# [(0, 1), (1, 0), (2, 0), (3, 1)]
查看您的示例输入
a = [[1,0,0,0],[0,0,1,1],[0,1,0,1],[1,0,0,1]]
和预期的输出
>>[(0, 1), (1, 0), (2, 0), (3, 1)]
您可以将问题重新定义为在每行中查找值为零的元素的索引(如果存在多个元素,return 第一个)。
通过这种方式构建,解决方案就像遍历 a
的每一行并检索值 0
的 index
一样简单(因此只有第一个元素会默认为 return。
使用如下所示的列表理解:
value_to_find = 0
desired_indexes = [
row.index(value_to_find) for row in a
]
或使用 map
将是:
value_to_find = 0
desired_indexes = map(lambda row:row.index(value_to_find), a)
然后您可以枚举它们以将结果与行号配对
enumerate(desired_indexes)
瞧瞧!
>>[(0, 1), (1, 0), (2, 0), (3, 1)]
整个解决方案可以像这样写在一行中:
answer = list(enumerate(map(lambda row:row.index(0), a)))
假设每一行都有一个零,你可以用
得到它的列索引c = np.argmin(a, axis=1)
或者,如果矩阵可以包含负数,你可以这样做
c = np.argmax(np.equal(a, 0), axis=1)
行正好
r = np.arange(len(a))
你想要的结果就是
result = np.stack((r, c), axis=-1)
如果有行中没有零,您可以使用掩码过滤结果:
mask = np.array(a)[r, c] == 0
result = result[mask, :]
此解决方案仅在每一行中至少有一个零时才有效。
indices = []
for x,row in enumerate(a):
i = row.index(0)
indices.append((x,i))
a[x] = row[:i+1] + [e for e in row[i:] if e]
print(indices)
print(a)
输出
[(0, 1), (1, 0), (2, 0), (3, 1)]
[[1, 0], [0, 1, 1], [0, 1, 1], [1, 0, 1]]