gpt4 book ai didi

python - 如何遍历枚举对象并使用 next() 函数打印所有索引项对?

转载 作者:行者123 更新时间:2023-11-28 19:45:48 25 4
gpt4 key购买 nike

我有以下程序

enum1 = enumerate("This is the test string".split())

我需要遍历 enum1 并使用 next() 函数打印所有索引项对。

我尝试通过执行以下操作来获取索引项对

for i in range(len(enum1)):
print enum1.next()

它抛出一个错误,显示 len() 不能应用于枚举。

谁能建议我使用 next() 函数遍历此枚举以打印所有索引项对的方法?

注意:我的要求是使用next() 函数来检索索引项对

最佳答案

只需使用:

for i,item in enum1:
# i is the index
# item is your item in enum1

for i,item in enumerate("This is the test string".split()):
# i is the index
# item is your item in enum1

这将使用下面的 next 方法...

关于python - 如何遍历枚举对象并使用 next() 函数打印所有索引项对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6411127/

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