#๐Ÿ”’ Numpy random array not able to output flattened array

14 messages ยท Page 1 of 1 (latest)

thick snow
#

Hi, I got a random array and tried repeat it several times to form a larger 1-D array.
However, when iterating the larger array to write to a file, it's still separated every a few elements which looks like there's still sub-array inside it. Any hint I can write it out in totally flattened way? Thanks

segment = np.random.randint(start_id, start_id+end_id-1, size=5, dtype="uint32")
out = np.tile(segment, 10000).flatten()

with open(outfile_path, "w") as f:
    writer = csv.writer(f)
    for i in out:
        print("Line: ", str(i))
        t = str(i)
        writer.writerow(t)

The weird looking result is like:

8,7,9,0,6,7
3,7,1,3,6
2,8,5,9,2,0
7,4,4,3,4,9
5,5,6,2,5,1
8,7,9,0,6,7
... ...

But I was expecting:
8
7
9
0
...

opaque templeBOT
#

@thick snow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

halcyon creek
#

segment should already be flat

#

i can't reproduce what you are getting

#

!e

import numpy as np
segment = np.random.randint(1, 10, size=5, dtype="uint32")
print(segment)```
opaque templeBOT
thick snow
halcyon creek
#

why are you using a csv writer for this?

#

!e

import numpy as np

segment = np.random.randint(1, 10, size=5, dtype="uint32")
out = np.tile(segment, 10)


with open("t.txt", "w") as f:
    for i in out:
        f.write(f"{i}\n")```
opaque templeBOT
thick snow
halcyon creek
opaque templeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.