简体   繁体   中英

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. 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 :

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Please advice how to achieve this?

This gives you all scores for players 1 and 2 combined:

>>> 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM