Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to repeat a list n times and reverse its elements alternately?

My input is a list of numbers that are not necessarily sorted but for the example I made a simple one :

my_list = [0, 1, 2, 3, 4]

I want to repeat it n times horizontaly but when i is odd we need to reverse the list :

# [[0, 4, 0, 4, 0, 4],
#  [1, 3, 1, 3, 1, 3],
#  [2, 2, 2, 2, 2, 2],
#  [3, 1, 3, 1, 3, 1],
#  [4, 0, 4, 0, 4, 0]]

For that I made the code below but I’m not proud of it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

final = []

for i in range(6):
    if i%2 == 0:
        final.append(my_list)
    else:
        final.append(my_list[::-1])
        
final = list(zip(*final))

I heard that numpy and pandas are more suitable for this kind of operations.

Do you guys have any idea how to do it ? I’m open to any suggestion.

>Solution :

Using ‘s tile (or repeat) and indexing:

my_list = [0, 1, 2, 3, 4]
N = 6

# repeat the array
out = np.tile(np.array(my_list)[:, None], (1, N))
# or
# out = np.repeat(np.array(my_list)[:, None], N, axis=1)

# invert the odd columns
out[:, 1::2] = out[::-1, 1::2]

Or with a small hack that might be faster for large N, for a pair of columns in the ascending/descending order, then tile it to the closest upper even value, and slice to remove the last column if even:

N = 6
a = np.array(my_list)
out = np.tile(np.c_[a, a[::-1]], (1, (N+1)//2))[:, :N]

Output:

array([[0, 4, 0, 4, 0, 4],
       [1, 3, 1, 3, 1, 3],
       [2, 2, 2, 2, 2, 2],
       [3, 1, 3, 1, 3, 1],
       [4, 0, 4, 0, 4, 0]])
timings

for N = 10_000:

# first approach
44.9 µs ± 1.14 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

# second approach
24.7 µs ± 1.86 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading