python 矩阵搜索
如果我有以下矩阵:
import numpy
ar = numpy.array((('0','1','2','3'), ('1','a','b','b'), ('2','b','c','d')), str)
print(ar)
输出:
[['0' '1' '2' '3']
['1' 'a' 'b' 'b']
['2' 'b' 'c' 'd']]
并且我想获取满足行和列条件的值。标题行是列(可以是字符串),左侧第一列是所有行(可以是字符串)。因此,如果我的列为“2”,行为“2”,我会得到“c”。我不知道“c”,只知道行和列的值。我该怎么做呢?
If I have the following matrix:
import numpy
ar = numpy.array((('0','1','2','3'), ('1','a','b','b'), ('2','b','c','d')), str)
print(ar)
Output:
[['0' '1' '2' '3']
['1' 'a' 'b' 'b']
['2' 'b' 'c' 'd']]
And I want to get the value where the condition for row and column is met. The header row are the columns (could be strings) and the first column on the left are all rows (could be strings). So if I have '2' for column and '2' for row I would get 'c'. I don't know 'c' yet only the values for rows and columns. How would I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 numpy.where :
并且:
You can use
numpy.where
:and:
如果您的意思是搜索“c”:
If you mean searching for 'c':
以上仅用于访问第 2 列第 2 行中的“c”,如果您想访问整个列,则必须:
The above is just for accessing the 'c' in column 2, row 2, if you want to access a whole column you would have to: