weatherman
weatherman

Reputation: 220

Indexing array from second element for all elements

I think it must be easy, but I cannot google it. Suppose I have array of numbers 1, 2, 3, 4.

import numpy as np

a = np.array([1,2,3,4])

How to index array if I want sequence 2, 3, 4, 1?? I know that for sequence 2, 3, 4 I can choose e.g.:

print(a[1::1])

Upvotes: 0

Views: 64

Answers (2)

Querenker
Querenker

Reputation: 2365

If you want to rotate the list, you can use a deque instead of a numpy array. This data structure is designed for this kind of operation and directly provides a rotate function.

>>> from collections import deque
>>> a = deque([1, 2, 3, 4])
>>> a.rotate(-1)
>>> a
deque([2, 3, 4, 1])

If you want to use Numpy, you can check out the roll function.

>>> import numpy as np
>>> a = np.array([1,2,3,4])
>>> np.roll(a, -1)
array([2, 3, 4, 1])

Upvotes: 1

weatherman
weatherman

Reputation: 220

One possible way is to define index set (a list).

index_set = [1, 2, 3, 0]
print(a[index_set])

Upvotes: 0

Related Questions