有没有办法在循环使用for循环时访问列表(或元组,或其他可迭代)的下一个或前一个元素?
l=[1,2,3] for item in l: if item==2: get_previous(l,item)
Markus Jarde.. 68
表示为生成器函数:
def neighborhood(iterable): iterator = iter(iterable) prev_item = None current_item = next(iterator) # throws StopIteration if empty. for next_item in iterator: yield (prev_item, current_item, next_item) prev_item = current_item current_item = next_item yield (prev_item, current_item, None)
用法:
for prev,item,next in neighborhood(l): print prev, item, next
Vicky Liau.. 29
一个简单的方法.
l=[1,2,3] for i,j in zip(l, l[1:]): print i, j
我使用了这个,但扩展了以避免删除开始/结束项:`for prev,cur,next in zip([None] + l [: - 1],l,l [1:] + [None]):` (10认同)
Rudiger Wolf.. 11
l=[1,2,3] for i,item in enumerate(l): if item==2: get_previous=l[i-1] print get_previous >>>1
Brian.. 8
在处理需要某些上下文的生成器时,我经常使用下面的实用程序函数在迭代器上给出一个滑动窗口视图:
import collections, itertools def window(it, winsize, step=1): """Sliding window iterator.""" it=iter(it) # Ensure we have an iterator l=collections.deque(itertools.islice(it, winsize)) while 1: # Continue till StopIteration gets raised. yield tuple(l) for i in range(step): l.append(it.next()) l.popleft()
它将一次生成序列N个项目的视图,将步骤移位.例如.
>>> list(window([1,2,3,4,5],3)) [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
在前瞻/后方情况下使用时,如果您还需要处理数字而没有下一个或上一个值,则可能需要使用适当的值(例如"无")填充序列.
l= range(10) # Print adjacent numbers for cur, next in window(l + [None] ,2): if next is None: print "%d is the last number." % cur else: print "%d is followed by %d" % (cur,next)
DuckPuncher.. 7
我知道这是旧的,但为什么不用enumerate
呢?
l = ['adam', 'rick', 'morty', 'adam', 'billy', 'bob', 'wally', 'bob', 'jerry'] for i, item in enumerate(l): if i == 0: previous_item = None else: previous_item = l[i - 1] if i == len(l) - 1: next_item = None else: next_item = l[i + 1] print('Previous Item:', previous_item) print('Item:', item) print('Next Item:', next_item) print('') pass
如果你运行它,你会看到它抓住上一个和下一个项目,而不关心重复列表中的项目.
表示为生成器函数:
def neighborhood(iterable): iterator = iter(iterable) prev_item = None current_item = next(iterator) # throws StopIteration if empty. for next_item in iterator: yield (prev_item, current_item, next_item) prev_item = current_item current_item = next_item yield (prev_item, current_item, None)
用法:
for prev,item,next in neighborhood(l): print prev, item, next
一个简单的方法.
l=[1,2,3] for i,j in zip(l, l[1:]): print i, j
l=[1,2,3] for i,item in enumerate(l): if item==2: get_previous=l[i-1] print get_previous >>>1
在处理需要某些上下文的生成器时,我经常使用下面的实用程序函数在迭代器上给出一个滑动窗口视图:
import collections, itertools def window(it, winsize, step=1): """Sliding window iterator.""" it=iter(it) # Ensure we have an iterator l=collections.deque(itertools.islice(it, winsize)) while 1: # Continue till StopIteration gets raised. yield tuple(l) for i in range(step): l.append(it.next()) l.popleft()
它将一次生成序列N个项目的视图,将步骤移位.例如.
>>> list(window([1,2,3,4,5],3)) [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
在前瞻/后方情况下使用时,如果您还需要处理数字而没有下一个或上一个值,则可能需要使用适当的值(例如"无")填充序列.
l= range(10) # Print adjacent numbers for cur, next in window(l + [None] ,2): if next is None: print "%d is the last number." % cur else: print "%d is followed by %d" % (cur,next)
我知道这是旧的,但为什么不用enumerate
呢?
l = ['adam', 'rick', 'morty', 'adam', 'billy', 'bob', 'wally', 'bob', 'jerry'] for i, item in enumerate(l): if i == 0: previous_item = None else: previous_item = l[i - 1] if i == len(l) - 1: next_item = None else: next_item = l[i + 1] print('Previous Item:', previous_item) print('Item:', item) print('Next Item:', next_item) print('') pass
如果你运行它,你会看到它抓住上一个和下一个项目,而不关心重复列表中的项目.
查看Tempita项目中的looper实用程序.它为循环项提供了一个包装器对象,它提供了上一个,下一个,第一个,最后一个等属性.
看一下looper类的源代码,很简单.还有其他这样的循环助手,但我现在不记得其他任何人.
例:
> easy_install Tempita > python >>> from tempita import looper >>> for loop, i in looper([1, 2, 3]): ... print loop.previous, loop.item, loop.index, loop.next, loop.first, loop.last, loop.length, loop.odd, loop.even ... None 1 0 2 True False 3 True 0 1 2 1 3 False False 3 False 1 2 3 2 None False True 3 True 0