1.简介
列表生成式即List Comprehensions,是Python中用于创建list的生成式。
2.示例
[表达式 循环体 条件语句]
#!/usr/bin/env python
# -*- coding: utf-8 -*- print [x * x for x in range(1, 11)]
print [x * x for x in range(1, 11) if x % 2 == 0]
print [m + n for m in 'ABC' for n in 'XYZ'] d = {'x': 'A', 'y': 'B', 'z': 'C'}
print [k + '=' + v for k, v in d.items()] L = ['Hello', 'World', 'IBM', 'Apple']
print [s.lower() for s in L]
结果如下: