首页 > 学院 > 开发设计 > 正文

List comprehension

2019-11-14 10:07:31
字体:
来源:转载
供稿:网友

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.


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表