有什么方法可以像 HashMap 中那样迭代 python/jython 列表吗?
示例:
list = [effectone, effecttwo, effectthree, effectfour]
if list.hasNext():
#do something
这就是我在 HashMap 中完成的方法:
it = channelList.entrySet().iterator() #channelList is an hashmap
if it.hasNext():
inext = it.next()
nextkey = inext.getKey() #Also, how do I get current "effect"?
nextvalue = inext.getValue()
提前致谢!
最佳答案
如果您想做的只是处理“数据对”,即:“下一个元素”有一个“下一个元素”,那么:
for cur, nxt in pairwise('abc'):
print cur, 'and', nxt
给你:
a and b
b and c
适应每次迭代对对象执行您需要执行的任何操作。
其中pairwise
是来自Python itertools documentation的配方像这样:
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
关于python - 如何在 Python/Jython 中迭代列表(如 HashMap)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20287956/