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

yipeiwu_com7年前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实现简单ftp客户端的方法

本文实例讲述了python实现简单ftp客户端的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/python # -*- coding: utf-8 -*- i...

Python如何获得百度统计API的数据并发送邮件示例代码

小工具 本来这么晚是不准备写博客的,当是想到了那个狗子绝对会在开学的时候跟我逼逼这个事情,所以,还是老老实实地写一下吧。 Baidu统计API的使用 系统环境: Python2...

python发送arp欺骗攻击代码分析

复制代码 代码如下:# -*- coding: cp936 -*-from scapy.all import *from threading import Thread,Lock,act...

Pandas中把dataframe转成array的方法

使用 df=df.values, 可以把Pandas中的dataframe转成numpy中的array 以上这篇Pandas中把dataframe转成array的方法就是小编分享给...

python3 实现验证码图片切割的方法

python3 实现验证码图片切割的方法

切割前图片 切割后四个图片 代码 #coding:utf8 import os from PIL import Image,ImageDraw,ImageFile import...