With the following example list: L = ['a','b','c','d']
I'd like to achieve the following output:
>>> a d b
>>> b a c
>>> c b d
>>> d c a
Pseudo-code would be:
for e in L:
print(e, letter_before_e, letter_after_e
You could just loop over L
and take the index i
minus and plus 1 modulo len(L)
to get the previous and next element.
You're pretty much there
for i, e in enumerate(L):
print(e, L[i-1], L[(i+1) % len(L)])
EDITED to add mod
it would probably be overkill in this case, but this is the general use-case for a circular doubly-linked list http://ada.rg16.asn-wien.ac.at/~python/how2think/english/chap17.htm
In my code I would use a moving window of 3 elements over the list prepended by the last element and appended by the first element:
from itertools import tee, izip, chain
def window(iterable,n):
'''Moving window
window([1,2,3,4,5],3) -> (1,2,3), (2,3,4), (3,4,5)
'''
els = tee(iterable,n)
for i,el in enumerate(els):
for _ in range(i):
next(el, None)
return izip(*els)
def chunked(L):
it = chain(L[-1:], L, L[:1]) # (1,2,3,4,5) -> (5,1,2,3,4,5,1)
for a1,a2,a3 in window(it,3): # (3,1,2,3,1) -> (3,1,2), (1,2,3), (2,3,1)
yield (a2,a1,a3)
## Usage example ##
L = ['a','b','c','d']
for t in chunked(L):
print(' '.join(t))
It's often simpler conceptually to keep track items you've already seen than to look ahead. The deque
class is ideal for keeping track of n
previous items because it lets you set a maximum length; appending new items automatically pushes old items off.
from collections import deque
l = ['a','b','c','d']
d = deque(l[-2:], maxlen=3)
for e in l:
d.append(e)
print d[1], d[0], d[2]
The only difference in this solution is that d c a
will come first instead of last. If that matters, you can start out as though you've already seen one iteration:
from collections import deque
l = ['a','b','c','d']
d = deque(l[-1:] + l[:1], maxlen=3)
for e in l[1:] + l[:1]:
d.append(e)
print d[1], d[0], d[2]