参考:列表生成式

Note

1.List Comprehensions,即列表生成式,是Python中内置的非常强大的list生成式。

eg.生成一个列表:[1*1, 2*2, ..., 10*10]

使用for...in的方法:

#!/usr/bin/env python3

L1 = []

for i in range(1, 11) :
L1.append(i*i) print(L1)

使用列表生成式:

L2 = [i*i for i in range(1, 11)]

print(L2)

output:

sh-3.2# ./listcomprehensions1.py
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

2.列表生成式可以在后面加上条件判断:

eg.符合 (i*i)%2==0 要求

L3 = [i*i for i in range(1, 11) if (i*i)%2 == 0]

print(L3)

output:

[4, 16, 36, 64, 100]

3.也可以使用两层循环:

L4 = [i+j for i in range(1, 11) for j in range(1, 11)]

print(L4)
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
L5 = [i+j for i in 'ABC' for j in 'abc']

print(L5)
['Aa', 'Ab', 'Ac', 'Ba', 'Bb', 'Bc', 'Ca', 'Cb', 'Cc']

4.使用列表生成式能简化代码,如输出当前目录的文件名:

import os

L6 = [i for i in os.listdir('.')] # listdir()

print(L6)
['listcomprehensions1.py']

5.对于dict来说,列表生成式也可以生成key-value的list:items()方法

dic = {'Chen': 'Student', '952693358': 'QQ', 'Never-give-up-C-X': 'WeChat'}

L7 = [x+'='+y for x, y in dic.items()]

print(L7)
['952693358=QQ', 'Chen=Student', 'Never-give-up-C-X=WeChat']

6.将字符串变成小写字符串:lower()函数

str1 = 'HeyGirl'

L8 = [i.lower() for i in str1]

print(L8)
['h', 'e', 'y', 'g', 'i', 'r', 'l']

Practice

如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,所以列表生成式会报错:

>>> L = ['Hello', 'World', 18, 'Apple', None]
>>> [s.lower() for s in L]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
AttributeError: 'int' object has no attribute 'lower'

使用内建的isinstance函数可以判断一个变量是不是字符串:

>>> x = 'abc'
>>> y = 123
>>> isinstance(x, str)
True
>>> isinstance(y, str)
False

提供:L1 = ['Hello', 'World', 18, 'Apple', None]

期待输出L2 = [L1中属于字符串的元素的小写]

Ans:

#!/usr/bin/env python3

L1 = ['Hello', 'World', 18, 'Apple', None]

L2 = [i.lower() for i in L1 if isinstance(i, str)]

print(L2)
sh-3.2# ./listcomprehensions2.py
['hello', 'world', 'apple']

2017/2/6

最新文章

  1. “You couldn’t see my tears cause I am in the water.“ Fish said to water.“But I could feel your tears cause you are in my heart..“ Answered water.
  2. Graphic32中TBitmap32.TextOut性能分析[转载]
  3. 最近公共祖先(lca)
  4. Unity中实现List类型的自定义GUI(ReorderableList)
  5. 基于HTML5的多张图片上传
  6. spring整合各大ORM框架的原理图
  7. win7 设置自动关机
  8. 第一次用Github desktop(mac)提交代码遇到的问题
  9. postgresql配置的一些问题
  10. 面试题:给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字
  11. 关于AIX VG中 LV 的状态问题,LV STATE
  12. 【程序员联盟】官网上线啦!coderunity.com
  13. 西邮linux兴趣小组2014纳新免试题(五)
  14. multiprocessing.Process() ----------python中的多进程
  15. Socket层实现系列 — send()类发送函数的实现
  16. GRU and LSTM
  17. PHP之缓存雪崩,及解决方法(转)
  18. Spark 论文篇-论文中英语单词集
  19. Ecshop表结构 order_info
  20. lunix nginx安装 报错页面 状态码

热门文章

  1. 剑指Offer——第一个只出现一次的字符位置
  2. 手动编译安装Libvirt之后利用systemctl管理libvirtd服务
  3. javascrpt 页面格式化页面
  4. 004-Shell 基本运算符、算术运算符、关系运算符、布尔运算符、辑运算符、字符串运算符、文件测试运算符
  5. python学习笔记(十五)异常处理
  6. 不受路径限制的 HALCON开发环境, 并且初始化两个Picture控件;
  7. Guid ToString 格式知多少?
  8. Linux系统——sed命令
  9. 《mysql必知必会》读书笔记--触发器及管理事务处理
  10. Git-创建和合并分支