列表中特定于Python索引的循环迭代
在本教程中,我们将编写一个程序来循环访问给定列表中的列表。让我们看一下解决问题的步骤
初始化列表和索引。
使用len查找列表的长度。
使用长度遍历列表。
使用index%length查找元素的索引。
打印元素。
递增索引。
这是一个简单的循环迭代。您可以编写它而没有任何麻烦。让我们看一下代码。
示例
# initializing the list and index alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] start_index = 5 # finding the length length = len(alphabets) # iterating over the list for i in range(length): # finding the index of the current element element_index = start_index % length # printing the element print(alphabets[element_index], end=' ') # incrementing the index for the next element start_index += 1
输出结果
如果运行上面的代码,则将得到以下结果。
f g h a b c d e
结论
如果您对本教程有任何疑问,请在评论部分中提及。
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短