[英]how can I convert position (2nd, 3rd or 4th etc ) to index(00 for 1st position, 01 for 2nd etc) in 2D array in python numpy?
import numpy as np;
entries = [];
# take user input
row = int(input("Enter number of rows: "));
column = int(input("Enter number of columns: "));
print("Enter Values");
# get values
for i in range(row):
a = [];
for j in range(column):
a.append(int(input()));
entries.append(a);
# convert values into matrix (dimentional array)
matrix = np.array(entries).reshape(row, column);
print("Matrix is :");
print(matrix);
我想知道逻辑,所以如果用户输入他想更改第 n 个位置的元素,如何将该位置更改为索引以执行更新操作?
假设用上面的代码用户创建这个数组
[[10 24 32
[45 56 62]]
之后他只想通过输入位置来更改第 4 个位置元素......如何将该位置转换为索引?
试试简单的数学:
import numpy as np
x = [[10 ,24 ,32],
[45 ,56 ,62]]
x = np.array(x)
pos, val = 4, -1
x[pos//x.shape[1], pos%x.shape[0]] = val
输出:
[[10 24 32]
[-1 56 62]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.