List comprehension

    xiaoxiao2021-03-26  22

    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.

    转载请注明原文地址: https://ju.6miu.com/read-659078.html

    最新回复(0)