简体   繁体   中英

retreive array value from text file and post it in array python

hello First i'm new on python What i want is to retreive from text file that containts set of array values :

This is my text files:

[0 1]   
[1 0] 
[1 1] 
[0 1] ....

i want to get an array that contains only the axes one

[[1],[0],[1],[1]]

and transofrm it to a vector :

 [ 1,0,1,1]

i did this code and it does not work

file = open('Score.txt','r')
for i in file:
    y_true = np.argmax(i, axis=1).transpose()
    print(y_true)

Here's how I'd get the second axis as a list, then you can do whatever you want with it. Assuming your example is the format for all data

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]

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