user1821176
user1821176

Reputation: 1191

np.savetxt from a created list error

consider the code:

dd21 = []

a = [1, 2, 3, 4]

for i in range(len(a)):

   for j in range(i+1, len(a)):

       dd21.append(a[i]-a[j])
       r = (a[i] -a[j])
       j = j + 1
       data1=np.column_stack((i,j,r))
       np.savetxt('lol.dat', data1)
       print i, j, r

output:

  0 2 -1

  0 3 -2

  0 4 -3

  1 3 -1

  1 4 -2

  2 4 -1 

Why don't I see the same list when I tried saving it in my lol.dat txt file?

Upvotes: 0

Views: 136

Answers (1)

HYRY
HYRY

Reputation: 97291

To save multiple arrays into one file, you can open the file first and call np.savetxt() with the file object:

dd21 = []

a = [1, 2, 3, 4]

with open("lol.dat", "w") as f:
    for i in range(len(a)):
       for j in range(i+1, len(a)):
           dd21.append(a[i]-a[j])
           r = (a[i] -a[j])
           j = j + 1
           data1=np.column_stack((i,j,r))
           np.savetxt(f, data1)
           print i, j, r

Or, you can concatenate all the arrays into a big array, and save it to the file.

Upvotes: 1

Related Questions