Say I have a list,
l = [1, 2, 3, 4, 5, 6, 7, 8]
I want to grab the index of an arbitrary element and the values of its neighbors. For example,
i = l.index(n)
j = l[i-1]
k = l[i+1]
However, for the edge case when i == len(l) - 1
this fails. So I thought I'd just wrap it around,
if i == len(l) - 1:
k = l[0]
else:
k = l[i+1]
Is there a pythonic way to do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…