简体   繁体   English

如何从文本文件的一列获取值以获取另一列的值

[英]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.

相关问题 相对于文本文件中另一列中的另一个值,逐个读取列中的每个值 - Read each value one by one in a column with respect to another values in another column from a text file 如何在另一列的条件下从一列中获取值的总和 - How to get the sum of values from one column with the conditional of another column 如何从值与另一列匹配的一列中获取最大值? - How can I get the max value from one column where values match another column? 仅使用标准库,按另一列的分组值中的一列的累积总数对文本文件进行排序? - Sorting a text file by cumulative total of one column from grouped values in another column using standard library only? 如何将多列从一个文本文件替换为另一文本文件中的列? - How to replace multi-column from one text file to a column in another text file? 从一列中获取对应于行子集的另一列的最小值的值 - Get values from one column corresponding to the minimum value of another column for a subset of rows 通过csv文件python中的另一列获取一列的值 - get value of one column by another column in csv file python 如何基于另一列值获取一列的值 - How to get the value of one column based on another column value 如何根据python(pandas,jupyter)中的另一列值获取一列的平均值 - how to get the average of values for one column based on another column value in python (pandas, jupyter) 如何遍历 CSV 文件并根据另一列的值更新一列中的值 - How to iterate over a CSV file and update values in one column based on the value of another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM