[英]How to get value from one column of a text file for values of another column
I am reading data from a text file. 我正在从文本文件读取数据。 The text file basically contains values like this 文本文件基本上包含这样的值
1 2 5
1 3 5
1 5 8
2 2 10
2 3 5
2 5 4
My code 我的密码
data = np.loadtxt('test.txt')
player = data.T[0]
position = data.T[1]
score = data.T[2]
Basically if i want to find all the scores of player 1 i will do score[player==1]
it will give me 5,5,10. 基本上,如果我想找到玩家1的所有得分,我会score[player==1]
这将给我5,5,10。 But i want to find all the score values that are common for two players so if i do this score[player==1 or player==2]
I get an error saying : 但是我想找到两个玩家共有的所有得分值,所以如果我执行此score[player==1 or player==2]
我会收到一条错误消息:
ValueError: The truth value of an array with more than one element is ambiguous. ValueError:具有多个元素的数组的真值不明确。 Use a.any() or a.all() 使用a.any()或a.all()
Please advice how to achieve this? 请指教如何实现这一目标?
This gives you all scores for players 1 and 2 combined: 这将为您提供玩家1和2结合的所有分数:
>>> score[np.logical_or(player==1, player==2)]
array([ 5., 5., 8., 10., 5., 4.])
If you are looking for the intersection of the scores between both players use: 如果您正在寻找两个球员之间的分数交集,请使用:
>>> np.intersect1d(score[player==1], score[player==2])
5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.