9

I'm looking to get an array (or matrix) that is circular, such that

Let:

 a = [1,2,3]

Then I would like

a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 1
a[4] = 2

Etc for all index values of a.

The reason is because I have an image as a matrix and what I'm trying to process it with the behaviour that if it goes off the edge in one direction, it should reappear on the opposite side.

Any tips on how to do this cleanly would be much appreciated!

2
  • itertools iterations Commented Apr 2, 2014 at 10:11
  • Wait, are they Python lists or numpy arrays? Commented Apr 2, 2014 at 10:26

3 Answers 3

12

You can use modulo operator, like this

print a[3 % len(a)] 

If you don't want to use modulo operator like this, you need to subclass list and implement __getitem__, yourself.

class CustomList(list):
    def __getitem__(self, index):
        return super(CustomList, self).__getitem__(index % len(self))

a = CustomList([1, 2, 3])
for index in xrange(5):
    print index, a[index]

Output

0 1
1 2
2 3
3 1
4 2

If you want to do the same with Numpy Arrays, you can do like this

import numpy as np

class CustomArray(np.ndarray):
    def __new__(cls, *args, **kwargs):
        return np.asarray(args[0]).view(cls)

    def __getitem__(self, index):
        return np.ndarray.__getitem__(self, index % len(self))

a = CustomArray([1, 2, 3])
for index in xrange(5):
    print a[index]

More information about Subclassing Numpy Arrays can be found here (Thanks to JonClements)

Sign up to request clarification or add additional context in comments.

2 Comments

The OP wants to keep looping again and again.
@sshashank124 Check the indexes in the question.
0

Having such functionality is not good for your code. Instead write a generator function which generates you round robin values.

numbers = [1, 2, 3]

def returnNumber():
    """
    A circular array for yielding list members repeatedly 
    """
    index = -1
    while True:
        index += 1
        yield slangWords[index % len(numbers)]

# Now you can use this generator
numberGenerator = returnNumber()
numberGenerator.next() # returns 1 
numberGenerator.next() # returns 2
numberGenerator.next() # returns 3
numberGenerator.next() # returns 1
numberGenerator.next() # returns 2

1 Comment

Late comment, but still. You should really include why you feel having said functionality is not good.
0

You can very simply:

mainArr = [5,2,1,4,2]
def getRangeArray(startIndexInMainArray):
    s = mainArr[startIndexInMainArray::]
    b=len(mainArr)-len(s)
    return (s+mainArr[0:b])

print(mainArr)
print(getRangeArray(4)) # What is the first index?

#for index 4
#[5, 2, 1, 4, 2]  before
#[2, 5, 2, 1, 4]  after

#for index 2
#[5, 2, 1, 4, 2]  before
#[1, 4, 2, 5, 2]  after

#for index 0
#[5, 2, 1, 4, 2]  before
#[5, 2, 1, 4, 2]  after

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.