簡體   English   中英

如何從文本文件的一列獲取值以獲取另一列的值

[英]How to get value from one column of a text file for values of another column

我正在從文本文件讀取數據。 文本文件基本上包含這樣的值

1 2 5
1 3 5
1 5 8
2 2 10
2 3 5
2 5 4

我的密碼

data = np.loadtxt('test.txt')
player = data.T[0]
position = data.T[1]
score = data.T[2]

基本上,如果我想找到玩家1的所有得分,我會score[player==1]這將給我5,5,10。 但是我想找到兩個玩家共有的所有得分值,所以如果我執行此score[player==1 or player==2]我會收到一條錯誤消息:

ValueError:具有多個元素的數組的真值不明確。 使用a.any()或a.all()

請指教如何實現這一目標?

這將為您提供玩家1和2結合的所有分數:

>>> score[np.logical_or(player==1, player==2)]
array([  5.,   5.,   8.,  10.,   5.,   4.]) 

如果您正在尋找兩個球員之間的分數交集,請使用:

>>> np.intersect1d(score[player==1], score[player==2])
5.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM