2020-04-22 16:45

# python（numpy）中，如何根据条件来修改某个矩阵中的值？

``````    import numpy as np
bay_7 = [[121, 7, 1, 1, 1], [122, 7, 2, 1, 1], [123, 7, 3, 1, 1], [124, 7, 4, 1, 1], [125, 7, 5, 1, 1], [126, 7, 1, 2, 0], [127, 7, 2, 2, 1],
[128, 7, 3, 2, 1], [129, 7, 4, 2, 1], [130, 7, 5, 2, 1], [131, 7, 1, 3, 0], [132, 7, 2, 3, 1], [133, 7, 3, 3, 1], [134, 7, 4, 3, 1],
[135, 7, 5, 3, 1], [136, 7, 1, 4, 0], [137, 7, 2, 4, 0], [138, 7, 3, 4, 1], [139, 7, 4, 4, 0], [140, 7, 5, 4, 0]]
bay_7 = np.array(bay_7)
x_1 = [133, 7, 3, 3, 0]
``````

``````    bay_7[np.where(bay_7[:, 0]==x_1[0])]=x_1
``````

``````    eval('bay_'+str(x_1[1])+'[np.where(bay_'+str(x_1[1])+'[:,0]==x_1[0])] = x_1')
``````

eval('bay_'+str(x_1[1])+'[np.where(bay_'+str(x_1[1])+'[:,0]==x_1[0])] = x_1')
File "", line 1
bay_7[np.where(bay_7[:,0]==x_1[0])] = x_1
^
SyntaxError: invalid syntax

• 写回答
• 好问题 提建议
• 关注问题
• 收藏
• 邀请回答

#### 1条回答默认 最新

• little student 2020-04-22 20:03
已采纳
``````eval('bay_' + str(x_1[1]))[np.where(eval('bay_' + str(x_1[1]))[:, 0]==x_1[0])]=x_1
``````
已采纳该答案
评论
解决 无用
打赏 举报