• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 跟老齐学Python之再深点,更懂list

跟老齐学Python之再深点,更懂list

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了跟老齐学python,跟老齐学python pdf,跟老齐学python下载,老齐 python,零基础学python 老齐等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

list解析

先看下面的例子,这个例子是想得到1到9的每个整数的平方,并且将结果放在list中打印出来

>>> power2 = []
>>> for i in range(1,10):
...   power2.append(i*i)
... 
>>> power2
[1, 4, 9, 16, 25, 36, 49, 64, 81]
</div>

python有一个非常有意思的功能,就是list解析,就是这样的:

>>> squares = [x**2 for x in range(1,10)]
>>> squares
[1, 4, 9, 16, 25, 36, 49, 64, 81]
</div>

看到这个结果,看官还不惊叹吗?这就是python,追求简洁优雅的python!

其官方文档中有这样一段描述,道出了list解析的真谛:

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
</div>

还记得前面一讲中的那个问题吗?

找出100以内的能够被3整除的正整数。
我们用的方法是:

aliquot = []

for n in range(1,100):
  if n%3 == 0:
    aliquot.append(n)

print aliquot

</div>

好了。现在用list解析重写,会是这样的:

>>> aliquot = [n for n in range(1,100) if n%3==0]
>>> aliquot
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
</div>

震撼了。绝对牛X!

其实,不仅仅对数字组成的list,所有的都可以如此操作。请在平复了激动的心之后,默默地看下面的代码,感悟一下list解析的魅力。

>>> mybag = [' glass',' apple','green leaf ']  #有的前面有空格,有的后面有空格
>>> [one.strip() for one in mybag]       #去掉元素前后的空格
['glass', 'apple', 'green leaf']
enumerate

</div>

这是一个有意思的内置函数,本来我们可以通过for i in range(len(list))的方式得到一个list的每个元素编号,然后在用list[i]的方式得到该元素。如果要同时得到元素编号和元素怎么办?就是这样了:

>>> for i in range(len(week)):
...   print week[i]+' is '+str(i)   #注意,i是int类型,如果和前面的用+连接,必须是str类型
... 
monday is 0
sunday is 1
friday is 2
</div>

python中提供了一个内置函数enumerate,能够实现类似的功能

>>> for (i,day) in enumerate(week):
...   print day+' is '+str(i)
... 
monday is 0
sunday is 1
friday is 2
</div>

算是一个有意思的内置函数了,主要是提供一个简单快捷的方法。

官方文档是这么说的:

Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence:
</div>
顺便抄录几个例子,供看官欣赏,最好实验一下。

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
</div>

在这里有类似(0,'Spring')这样的东西,这是另外一种数据类型,待后面详解。

</div>

您可能想查找下面的文章:

  • 跟老齐学Python之使用Python查询更新数据库
  • 跟老齐学Python之使用Python操作数据库(1)
  • 跟老齐学Python之编写类之二方法
  • 跟老齐学Python之编写类之一创建实例
  • 跟老齐学Python之关于类的初步认识
  • 跟老齐学Python之传说中的函数编写条规
  • 跟老齐学Python之总结参数的传递
  • 跟老齐学Python之变量和参数
  • 跟老齐学Python之重回函数
  • 跟老齐学Python之Python文档

相关文章

  • Python深入学习之对象的属性
  • 浅谈Python的Django框架中的缓存控制
  • django轻松使用富文本编辑器CKEditor的方法
  • Python实现TCP/IP协议下的端口转发及重定向示例
  • Python实现简单的可逆加密程序实例
  • Python中的with...as用法介绍
  • 用Python实现通过哈希算法检测图片重复的教程
  • 玩转python爬虫之正则表达式
  • Python线程指南详细介绍
  • python检测远程服务器tcp端口的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python中操作mysql的pymysql模块详解
    • 打开电脑上的QQ的python代码
    • Python采集腾讯新闻实例
    • 使用Python的urllib2模块处理url和图片的技巧两则
    • python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
    • python实现获取客户机上指定文件并传输到服务器的方法
    • 爬山算法简介和Python实现实例
    • python条件和循环的使用方法
    • Python中__init__和__new__的区别详解
    • 关于numpy中np.nonzero()函数用法的详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有