List comprehension

来源:互联网 发布:linux dd 备份u盘 编辑:程序博客网 时间:2024/05/21 19:43

List Comprehension

from: http://www.bogotobogo.com/python/python_list_comprehension.php

List comprehension

if - for

[24.0 for x in xrange(len(df)) if df[x] > 24.0]

if - else - for
[24.0 if df[x] > 24.0 else df[x] for x in xrange(len(df))]

Example
Order['twenfour'] = [1.0 if ((Order.iloc[i,3] - Order.iloc[i,2]).total_seconds()) > delta else 0.0 for i in xrange(len(Order))]

tips:
List comprehension run at C Speed inside the interpreter, so it is much faster than for loop. However, for loops make logic more explicit.

0 0