2

I have a circular array. I created it with the following:

from itertools import cycle
myArray = ['a','b','c','d']
pool = cycle(myArray)

Now I want to print the nth item in pool where n could be larger than 4. Normally this would be a simple use of the modulo function but logically I think Python has a method which will know the number of elements in the pool (4 in this example) and automatically apply the modulo function.

For example the 1st and 5th item is 'a'. So I'm hoping for, logically, the equivalent of pool[0] and pool[4] giving me 'a'.

Is there such a method?

4
  • 4
    Wouldn't myArray[n % 4] achieve the same thing? (I guess I'm asking if you really need to pull lots of items out of the generator just to achieve something that could be done a simpler way.) Commented Dec 26, 2015 at 22:09
  • 3
    Mind that pool is not a circular array: it is a generator. Commented Dec 26, 2015 at 22:11
  • You're really not making any sense as to what you want. Commented Dec 26, 2015 at 22:12
  • 1
    Yes, I already said modulo could be used. If you think I'm not making sense please read it more carefully. Everyone else made sense it. Commented Dec 27, 2015 at 13:15

4 Answers 4

5

No, there's no built-in method to accomplish what you're attempting to do. As suggested earlier, you could use zip, but that would involve indexing into the result based on your sequence, as well as generating n elements out to the item you want.

Sometimes the simplest approach is the clearest. Use modulo to accomplish what you're after.

def fetch_circular(n):
    myArray = ['a','b','c','d']
    return myArray[n % 4]
Sign up to request clarification or add additional context in comments.

Comments

4

I think you may be confusing arrays with generators.

The modulo function of an array is the way to go, in terms of performance.

cycle is a function which generates elements as they are requested. It is not a Cycle class with convenient methods. You can see the equivalent implementation in the documentation, and you'll probably understand what is the idea behind it:

https://docs.python.org/2/library/itertools.html#itertools.cycle

Comments

2

A list is definitely the way to go but if you actually had a cycle object and wanted the nth object wrapping around, you could islice:

from itertools import cycle, islice
myArray = ['a','b','c','d']
pool = cycle(myArray)

print(next(islice(pool, 5)))
a

Note once you call next(islice you have started cycling the list, if you actually want to be constantly rotating you may actually want a deque

1 Comment

I think there is a difference between knowing the answer, and answering what you wanted to hear ;)
0

Your pool object is already a generator and it will keep looping through myArray forever, so all you need is to zip your iterator with pool this way:

>>> pool = cycle(myA)
>>> for i,item in zip(range(10),pool):
    print i,item


0 a
1 b
2 c
3 d
4 a
5 b
6 c
7 d
8 a
9 b
>>> 

1 Comment

A potential problem with this approach is that if you ask for the 10G'th element, it will take some time whereas using the modulo approach, it is resolved in nanoseconds.

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.