[英]retreive array value from text file and post it in array python
你好,首先我是python的新手,我想要從包含數組值集合的文本文件中檢索:
這是我的文本文件:
[0 1]
[1 0]
[1 1]
[0 1] ....
我想得到一個只包含一個軸的數組
[[1],[0],[1],[1]]
並將其轉換為向量:
[ 1,0,1,1]
我做了這段代碼,它不起作用
file = open('Score.txt','r')
for i in file:
y_true = np.argmax(i, axis=1).transpose()
print(y_true)
這是我將第二個軸作為列表的方式,然后您可以使用它進行任何操作。 假設您的示例是所有數據的格式
with open(r"Score.txt", "r") as f:
lines = f.readlines()
res = [int(x.split()[1].replace("]","")) for x in lines]
>>> res
[1, 0, 1, 1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.