Python实现带下标索引的遍历操作示例

本文实例讲述了Python实现带下标索引的遍历操作。分享给大家供大家参考,具体如下:

代码如下:

#coding=utf-8
#python - 实现带下标索引的遍历.
str = 'abcdefghigklmn'
#方式一:for
i = 0
for ch in str:
  print('%d\t%s'%(i,ch))
  i+=1
print('-'*50)
#方式二:enumerate()
for i,ch in enumerate(str):
  print i,ch

运行结果:

0   a
1   b
2   c
3   d
4   e
5   f
6   g
7   h
8   i
9   g
10  k
11  l
12  m
13  n
--------------------------------------------------
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 i
9 g
10 k
11 l
12 m
13 n

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python字符串操作技巧汇总》、《Python字典操作技巧汇总》、《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

您可能感兴趣的文章:
  • python通过索引遍历列表的方法
  • python同时遍历数组的索引和值的实例
  • python两种遍历字典(dict)的方法比较
  • Python中使用item()方法遍历字典的例子
  • Python字符遍历的艺术
  • Python openpyxl 遍历所有sheet 查找特定字符串的方法
  • python 遍历字符串(含汉字)实例详解
  • Python 遍历列表里面序号和值的方法(三种)
  • Python递归遍历列表及输出的实现方法
  • Python中使用遍历在列表中添加字典遇到的坑

转载请注明出处:http://www.bqysc.com/article/20230526/233440.html