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.
新闻热点
疑难解答