gpt4 book ai didi

python - 如何在 Python/Jython 中迭代列表(如 HashMap)?

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:13 24 4
gpt4 key购买 nike

有什么方法可以像 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com