how can I get python's np.savetxt to save each iteration of a loop in a different column?
Question: This is an extremely basic code that does what I want... except with regard to
the writing of the text file.

    
    
    import numpy as np
    
    f = open("..\myfile.txt", 'w')
    tst = np.random.random(5)
    tst2 = tst/3
    for i in range(3):
        for j in range(5):
            test = np.random.random(5)+j
            a = np.random.normal(test, tst2)
        np.savetxt(f, np.transpose(a), fmt='%10.2f')
        print a
    f.close()
    

This code will write to a .txt file a single column that is concatenated after
each iteration of the for loop.

**What I want is independent columns for each iteration.**

**How does one do that?**

note: I have used `np.c_[]` as well, and that _will_ write the columns **if**
I express each iteration within the command. ie: `np.c_[a[0],a[1]]` and so on.
The problem whit this is, what if both my `i` and `j` values are very large?
It isn't reasonable to follow this method.


Answer: So a run produces:

    
    
    2218:~/mypy$ python3 stack39114780.py 
    [ 4.13312217  4.34823388  4.92073836  4.6214074   4.07212495]
    [ 4.39911371  5.15256451  4.97868452  3.97355995  4.96236119]
    [ 3.82737975  4.54634489  3.99827574  4.44644041  3.54771411]
    2218:~/mypy$ cat myfile.txt
          4.13
          4.35
          4.92
          4.62
          4.07    # end of 1st iteration
          4.40
          5.15
          4.98
          3.97
          ....
    

Do you understand what's going on? One call to `savetxt` writes a set of
lines. With a 1d array like `a` it prints one number per row. (`transpose(a)`
doesn't do anything).

File writing is done line by line, and can't be rewound to add columns. So to
make multiple columns you need to create an array with multiple columns. Then
do one `savetxt`. In other words, collect all the data before writing.

Collect your values in a list, make an array, and write that

    
    
    alist = []
    for i in range(3):
        for j in range(5):
            test = np.random.random(5)+j
            a = np.random.normal(test, tst2)
            alist.append(a)
    arr = np.array(alist)
    print(arr)
    np.savetxt('myfile.txt', arr, fmt='%10.2f')
    

I'm getting 15 rows of 5 columns, but you can tweak that.

    
    
    2226:~/mypy$ cat myfile.txt
      0.74       0.60       0.29       0.74       0.62
      1.72       1.62       1.12       1.95       1.13
      2.19       2.55       2.72       2.33       2.65
      3.88       3.82       3.63       3.58       3.48
      4.59       4.16       4.05       4.26       4.39
    

Since `arr` is now 2d, `np.transpose(arr)` does something meaningful - I would
get 5 rows with 15 columns.

==================

With

    
    
    for i in range(3):
        for j in range(5):
            test = np.random.random(5)+j
            a = np.random.normal(test, tst2)
        np.savetxt(f, np.transpose(a), fmt='%10.2f')
    

you write `a` once for each `i` \- hence the 3 rows. You throwing away 4 of
the `j` iterations. In my variation I collect all `a`, and hence get 15 rows.

