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

标签 python list loops jython

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

相关文章:

python - Py 安装程序 "ImportError: No module named Pyinstaller"

python - (Python : discord. py)错误 : Could not build wheels for multidict, 使用 PEP 517 且无法直接安装的 yarl

python - Pygame 显示镜像的图像字母

python - 使用 cython 或 PyPy 优化元组/列表(用 python 实现的图论算法)

java - 如何在 Java 中使用 Comparables 创建自定义参数化列表对象?

javascript - 使用串联和传递的参数循环遍历数组

javascript - 使用 Python 检索 javascript 文件内容

python - 需要备用 Python 列表反向解决方案

c++ - C++ 上的用户输入结束循环

Bash 脚本在 while 循环中停止执行 ffmpeg - 为什么?