Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping around a list as a slice operation

Tags:

python

slice

Consider the following simple python code

>>> L = range(3)
>>> L
[0, 1, 2]

We can take slices of this array as follows:

>>> L[1:3]
[1, 2]

Is there any way to wrap around the above array by shifting to the left

[1, 2, 0]

by simply using slice operations?

like image 310
D R Avatar asked Dec 24 '10 23:12

D R


People also ask

How do you slice a list?

You can generate a slice object using the built-in function slice() . If you want to repeatedly select the items at the same position, you only need to generate the slice object once. slice(start, stop, step) is equivalent to start:stop:step . If two arguments are specified, step is set to None .

Is slicing a list operation?

With this operator, one can specify where to start the slicing, where to end, and specify the step. List slicing returns a new list from the existing list. If Lst is a list, then the above expression returns the portion of the list from index Initial to index End, at a step size IndexJump.

What is slicing on a string or list?

The term slicing in programming usually refers to obtaining a substring, sub-tuple, or sublist from a string, tuple, or list respectively. Python offers an array of straightforward ways to slice not only these three but any iterable. An iterable is, as the name suggests, any object that can be iterated over.

How do you wrap around in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.


3 Answers

Rotate left n elements (or right for negative n):

L = L[n:] + L[:n]

Note that collections.deque has support for rotations. It might be better to use that instead of lists.

like image 119
Mark Byers Avatar answered Oct 20 '22 20:10

Mark Byers


Left:

L[:1], L[1:] = L[-1:], L[:-1]

Right:

L[-1:], L[:-1] = L[:1], L[1:]
like image 32
codewarrior Avatar answered Oct 20 '22 21:10

codewarrior


To my mind, there's no way, unless you agree to cut and concatenate lists as shown above. To make the wrapping you describe you need to alter both starting and finishing index.

  • A positive starting index cuts away some of initial items.
  • A negative starting index gives you some of the tail items, cutting initial items again.
  • A positive finishing index cuts away some of the tail items.
  • A negative finishing index gives you some of the initial items, cutting tail items again.

No combination of these can provide the wrapping point where tail items are followed by initial items. So the entire thing can't be created.

Numerous workarounds exist. See answers above, see also itertools.islice and .chain for a no-copy sequential approach if sequential access is what you need (e.g. in a loop).

like image 44
9000 Avatar answered Oct 20 '22 21:10

9000