0

I have got my array like this :

[[-3.75536609e+00  9.41899478e-01  1.59805989e+00 -8.97728577e-02
  -3.67324662e+00  2.85971731e-01]
 [-1.48593640e+00  6.56555116e-01  6.41966641e-01 -2.62360811e-01
  -4.66498184e+00 -8.62938017e-02]
 [ 7.05197873e-03 -4.12116805e-03 -1.30933505e-02  5.76611329e-03
  -6.51661423e-04 -9.73143987e-03]
 ...
 [ 7.28487849e-01 -2.02445209e-01 -1.56245055e-02 -1.48709917e+00
  -4.94436502e+00 -1.88474905e+00]
 [ 6.85976565e-01 -1.20424531e-01 -2.20480785e-01 -1.23380101e+00
  -3.60452390e+00 -1.54651344e+00]
 [ 1.47314686e-02  1.42228836e-02 -1.20135369e-02 -4.32784623e-03
  -8.07471294e-03  2.29109559e-04]]
[-0.6025902  -0.31237862 -1.2150506   3.072987   -1.3560013  -1.0883616 ]

I want to be able to write this array to a text file. Each element should be printed line by line. For instance, the first one could look like this

-3.75536609e+00 
 9.41899478e-01 
 1.59805989e+00 
-8.97728577e-02
 -3.67324662e+00 
 2.85971731e-01

How do I do this?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
pink puffles
  • 43
  • 1
  • 8
  • 2
    Flatten your array and save it in a text file – Sheldore Aug 31 '18 at 18:22
  • 1
    This looks like it only has two dimensions, not three. Do you know how loops work? How do you intend to represent the dimensions in your text file? – ChrisGPT was on strike Aug 31 '18 at 18:23
  • @Chris I don't care about the dimensions or anything. Just want to get it printed line by line – pink puffles Aug 31 '18 at 18:26
  • @Chris Can you please give me a link to resource where I can study these dimension stuff. I don't understand how this is 2d – pink puffles Aug 31 '18 at 18:27
  • Then just do exactly what @Bazingaa suggests: call `flatten` on the array, and `np.savetxt` the result, and you're done. – abarnert Aug 31 '18 at 18:28
  • `print (functools.reduce(lambda a,b : a+b,lis))` might be useful to flattent the list – mad_ Aug 31 '18 at 18:29
  • 2
    1D means you just have a bunch of values. 2D means you have a bunch of rows, each of which is a bunch of values-that's what you have here. 3D means you have a bunch of layers, each of which is a bunch of rows, each of which is a bunch of values. – abarnert Aug 31 '18 at 18:30
  • @abarnert Awesome explanation! Thanks! – pink puffles Aug 31 '18 at 18:30
  • @mad_ That's an inefficient and overly complicated way to do it. It's just a fancy way of spelling `sum`, and there's a reason `sum` doesn't like working on sequences, but then you've added extra function call overhead on top of that. – abarnert Aug 31 '18 at 18:30
  • Just use `arr.flatten()` where `arr` is your array and [this](https://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html) is how you use `savetxt` – Sheldore Aug 31 '18 at 18:31
  • @Bazingaa But this is clearly a numpy array, so why show the OP how to flatten without using numpy instead of just calling `flatten` on it? – abarnert Aug 31 '18 at 18:31
  • @Chris Look at the output. No commas between the columns or the rows, padding, etc.—that's exactly what `np.ndarray.__str__` gives you, but there's no way to get that out of a list of lists (without writing your own code out of `join` and the like, which I doubt the OP did). – abarnert Aug 31 '18 at 18:32
  • @Bazingaa this is how I have flattened it for x in h: for y in x: flattened_list.append(y) np.savetxt('wts.txt',flattened_list,delimiter=" ", fmt="%s"). However, some of it gets flattened, some of it doesn't – pink puffles Aug 31 '18 at 18:33
  • @Chris lists *do not have dimensions*, `numpy.ndarray` objects, on the other hand, do. – juanpa.arrivillaga Aug 31 '18 at 18:43
  • @juanpa.arrivillaga, it might not "have" dimensions in the sense of having them encoded into a class, but a list is definitely a one-dimensional data structure. – ChrisGPT was on strike Aug 31 '18 at 18:44
  • @Chris yes, I can agree with that. `list` objects have a length, so saying it is "one dimensional" makes sense. Where I disagree is saying that a list-of-lists has "two dimensions". Obviously, we are talking about abstractions, and we can conceptualize of these things flexibly, and if in some context it helps to think of a lists-of-lists as a "two-dimensional list" so be it. – juanpa.arrivillaga Aug 31 '18 at 18:48
  • @juanpa.arrivillaga, I haven't worked with NumPy (though I'm familiar with data frames and such from R) and I didn't recognize that that's what we were talking about until others pointed it out since the question wasn't properly tagged. A list of lists requires two indices to uniquely identify an element contained in the inner lists. It certainly isn't perfect, but IMO it's a decent illustration using built-in data structures. I'll remove my comment. – ChrisGPT was on strike Aug 31 '18 at 18:50

2 Answers2

0

If these are numpy arrays, you can use np.savetxt with delimiter='\n', and enumerate through your arrays:

Given array x:

>>> x
array([[-3.75536609e+00,  9.41899478e-01,  1.59805989e+00,
        -8.97728577e-02, -3.67324662e+00,  2.85971731e-01],
       [-1.48593640e+00,  6.56555116e-01,  6.41966641e-01,
        -2.62360811e-01, -4.66498184e+00, -8.62938017e-02],
       [ 7.05197873e-03, -4.12116805e-03, -1.30933505e-02,
         5.76611329e-03, -6.51661423e-04, -9.73143987e-03]])

Use:

[np.savetxt(f'array{n}.txt',i,delimiter='\n') for n,i in enumerate(x)]

And you'll get 3 text files:

array0.txt will be:

-3.755366089999999879e+00
9.418994779999999567e-01
1.598059890000000038e+00
-8.977285769999999998e-02
-3.673246620000000018e+00
2.859717310000000068e-01

etc...

[EDIT] It seems I misunderstood your question. If you just want one txt file, use:

np.savetxt('array.txt', x.flatten(), delimiter='\n')

Which will give array.txt:

-3.755366089999999879e+00
9.418994779999999567e-01
1.598059890000000038e+00
-8.977285769999999998e-02
-3.673246620000000018e+00
2.859717310000000068e-01
-1.485936399999999935e+00
6.565551160000000497e-01
6.419666410000000045e-01
-2.623608110000000271e-01
-4.664981840000000268e+00
-8.629380170000000572e-02
7.051978730000000098e-03
-4.121168050000000374e-03
-1.309335049999999986e-02
5.766113290000000370e-03
-6.516614230000000382e-04
-9.731439870000000575e-03
sacuL
  • 49,704
  • 8
  • 81
  • 106
  • I have a large number of arrays in the order of thousands. This will generate thousands of files – pink puffles Aug 31 '18 at 18:42
  • 1
    From your question, I gathered that is what you wanted. However, if you just want one txt file, use: `np.savetxt('array.txt', x.flatten(), delimiter='\n')` (see edits) – sacuL Aug 31 '18 at 18:47
0

from this solution

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)):
            yield from flatten(el)
        else:
            yield el

create a simple list. Then dump it to file:

with open(outfile) as f:
    f.write("\n".join(flatlist))
VanTan
  • 617
  • 4
  • 12