python技巧 列表推导
程序员文章站
2023-10-06 09:42:27
val = [expression for value in collection if condition]等价于val = []for value in collection: if condition: val.append(expression)列表推导在python2中存在变量泄漏的问题: ......
val = [expression for value in collection if condition]
等价于
val = []
for value in collection:
if condition:
val.append(expression)
列表推导在python2中存在变量泄漏的问题:
>>> i=1
>>> [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> i
9
python 3中已经修复:
in [2]: i=1
in [3]: [i for i in range(10)]
out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
in [4]: i
out[4]: 1
上一篇: 有些人自己不好看
下一篇: mysql索引创建和使用优缺点讲解