迭代
首先理解下什么是迭代,python中所有从左往右扫面对象的方式都是可迭代的
有哪些方式是可迭代的:
1.文件操作
我们读取文件的时候,会用到一个readline()方法,其实它就是一个迭代器,它会返回当前的数据,然后自动的调用内置的next()方法来让文件的读取头自动的移动到当前的下面一行,准备下次的读取,到达文件末尾时,就会返回空字符串.
>>> f=open('hello.py') >>> f.readline() '#!/usr/bin/python2.5\n' >>> f.readline() 'print "hello.word!"\n' >>> f.readline() '\n' >>> f.readline() '' >>> for i in open('hello.py'): ... print(i) ... #!/usr/bin/python2.5 print "hello.word!"
用上面这样方式来读取文件内容的话,速度很快,内存占用也比较低,特别适合操作大文件.
下面这个方式适合操作一些小的文件,速度和效率没有上面的好,所以建议以后操作文件的话,尽量用上面的。
>>> for i in open('hello.py').readlines(): ... print i ... #!/usr/bin/python2.5 print "hello.word!"
read方法和readline方法,
read()方法把整个文件的内容放到字符串里
readline()方法则把文件的内容按照行为单位放到列表里。
一般要替换文件里的某个字符的话,最好有readline,然后用循环把一行一行内容循环出来,再查找替换,这样效率比整个读到一个字符串里来查找匹配效果更高。
2 for循环
例如:
>>> for i in range(5): ... print(i) ...
它中间处理的过程和下面的是一样的:
>>> L=[0,1,2,3,4] >>> I=iter(L) >>> I.next() 0 >>> I.next() 1 >>> I.next() 2 >>> I.next() 3 >>> I.next() 4 >>> I.next()
Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
每次调用迭代器调用next()方法返回结果,并让文件指针往下移动一行,最后已StopIteration异常结束迭代。
3.列表解析:
相比python for循环速度会快很多
例如:
>>> L=[x+10 for x in range(10)] >>> L
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
python会在解释器里对range(10)进行迭代,依次把列表里的内容取出来,赋值给最左边的x,然后执行x+10的操作,
并且把执行好的结果保存在列表里。等range(10)迭代完以后就新生成了一个列表,结果就是[10,11,12,13,14,15,16,17,18,19]
从上面可以看出,这也是建立python 列表的一个方法。
上面例子也可以用for循环来实现.
>>> res=[] >>> for x in range(10): ... res.append(x+10) ... >>> res [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
从上面可以看出,python列表解析比手动的for 更加精简,而且运行的更快(往往速度回快一倍),因为他们的迭代在解析器内部是以C语言的速度执行的,而不是以手动python代码执行的,特别对于较大的数据集合,这是使用列表解析的一个主要的性能优点.
遍历
1.通过序列取元素的方法进行遍历
root@10.1.6.200:python# vim 3.py
#!/usr/bin/python2.5 for i in 'hello': #序列里的字符串 print i, y = [1,2,3,4,5,6] #列表 for i in y: print i,
root@10.1.6.200:python# python 3.py
h e l l o 1 2 3 4 5 6
2.通过序列本身偏移指数(索引)的方法进行遍历
也就是迭代序列索引,注:迭代,重复执行一条指令.
root@10.1.6.200:python# vim 3.py
#!/usr/bin/python2.5 x='hello' for i in range(len(x)): print x[i] y = [1,2,3,4,5,6] for i in range(len(y)): print y[i],
root@10.1.6.200:python# python 3.py
h e l l o 1 2 3 4 5 6
字典有2种方式取到其值:
1.先取字典key,在取索引的值
root@10.1.6.200:python# vim 5.py
#!/usr/bin/python2.5 z = {1:'a',2:'b',3:'c'} for i in z: print z[i]
root@10.1.6.200:python# python 5.py
a b c
2.通过字典items方法,获取所有键值对,在利用元组拆分的方法获得对应值.
root@10.1.6.200:python# cat 5.py
#!/usr/bin/python2.5 z = {1:'a',2:'b',3:'c'} print z.items() for m,n in z.items(): print m,n
root@10.1.6.200:python# python 5.py
[(1, 'a'), (2, 'b'), (3, 'c')] 1 a 2 b 3 c

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools
