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

作者:hebedich 时间:2021-02-05 21:44:18 

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]

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


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

看到这个结果,看官还不惊叹吗?这就是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.

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

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


aliquot = []

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

print aliquot

好了。现在用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]

震撼了。绝对牛X!

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


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

这是一个有意思的内置函数,本来我们可以通过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

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


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

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

官方文档是这么说的:


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:


顺便抄录几个例子,供看官欣赏,最好实验一下。


>>> 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')]

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

标签:跟老齐学Python,更懂list
0
投稿

猜你喜欢

  • CSS网页布局扩展小技巧

    2010-06-03 12:13:00
  • 使用php语句将数据库*.sql文件导入数据库

    2023-11-23 05:11:22
  • 详解Python用三种方式统计词频的方法

    2021-02-10 13:30:12
  • MySQL也能并发导入数据

    2010-03-25 10:38:00
  • Python pickle模块常用方法代码实例

    2023-12-25 03:09:16
  • python中join与os.path.join()函数实例详解

    2023-08-23 19:20:51
  • python中os和sys模块的区别与常用方法总结

    2022-05-26 18:04:24
  • ASP实例:读取xml文件的程序

    2007-11-04 18:47:00
  • javascript分页代码实例分享(js分页)

    2023-10-11 10:00:57
  • Pytorch使用MNIST数据集实现基础GAN和DCGAN详解

    2021-11-17 02:14:33
  • 完全讲解 使用MSCS建立SQL Server集群

    2009-01-19 14:10:00
  • 清除浮动新说

    2009-12-25 18:49:00
  • Python和Bash结合在一起的方法

    2023-11-02 20:53:27
  • oracle 触发器 学习笔记

    2009-05-24 19:57:00
  • 在Pycharm中对代码进行注释和缩进的方法详解

    2023-09-27 23:55:26
  • 在python中使用requests 模拟浏览器发送请求数据的方法

    2022-05-05 03:17:35
  • ASP程序中输出Excel文件实例一则

    2008-11-07 15:29:00
  • asp如何对一个文件夹进行创建和删除?

    2009-11-20 18:42:00
  • js数组与字符串的相互转换方法

    2023-06-24 09:41:48
  • 深入了解python的函数参数

    2023-07-24 08:19:01
  • asp之家 网络编程 m.aspxhome.com