[Tutor] Python question

Cameron Simpson cs at cskk.id.au
Tue Apr 19 19:53:48 EDT 2022


On 18Apr2022 11:50, Eleonora Panini <eleonora.panini7jkd at gmail.com> wrote:
><https://stackoverflow.com/posts/71904558/timeline>
>I need help to do the following steps in Phyton:
>
>-my starting array is: array = np.random.rand(1024x11700)
>
>-I have to obtain an array 1024x23400 with the following characteristics:
>25 columns(1024x25) of data and 25 columns (1024x25) of zeros
>
> So I thought to do a loop every 25 columns(1024x25), insert an array of
>zeros (1024x25) alternatevely every 25 columns, so I obtain an array
>1024x23400..
>
>but I don't know how to do it in Phyton.. Can you help me? Thank you

See Dennis' followup with suggested numpy functions which will help.

My own naive approach would be to make a new array by alternatively 
concatentating your 25 rows of data, then 25 rows of zeroes (Dennis 
mentioned a .zeroes function), repeat until you have done the whole 
original array.

I suggest you use a for-loop, using a range() to obtain the offsets you 
need: https://docs.python.org/3/library/stdtypes.html#range
so the initial loop would look like this:

    for offset in range(0, 11700, 25):

which counts "offset" from 0 to 11700 (excluding the 11700) in steps of 
25. That would let you grab the 25 rows of data at each offset and make 
an additional 25 rows of zeroes for building the second array.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list