1.列表推导式
In [1]: [i**2 for i in range(1,5)] Out[1]: [1, 4, 9, 16]
2.字典推导式
In [2]: {i:i**2 for i in range(1,5)} Out[2]: {1: 1, 2: 4, 3: 9, 4: 16}
3.集合推导式
In [3]: { i for i in range(1,5)} Out[3]: {1, 2, 3, 4}
4.生成器推导式
In [11]: items = (i**2 for i in range(1,5)) In [12]: for item in items: ...: print(item) ...: 1 4 9 16
原文地址:https://www.cnblogs.com/litao2011/p/9549234.html
时间: 2024-11-06 23:53:26