Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为此for循环创建列表理解_Python_List_List Comprehension - Fatal编程技术网

Python 如何为此for循环创建列表理解

Python 如何为此for循环创建列表理解,python,list,list-comprehension,Python,List,List Comprehension,我试图用列表理解来代替这个for循环。我的名单是 test_list = [3, 4, 6, 3, 8, 4, 7, 8, 12, 14, 1, 6, 7, 3, 7, 8, 3, 3, 7] 功能是 import numpy as np def ema(x, n): x = np.array(x) emaint = np.zeros(len(x)) k = 2 / float(n + 1) emaint[0:n] = np.average(x[:n])

我试图用列表理解来代替这个for循环。我的名单是

test_list = [3, 4, 6, 3, 8, 4, 7, 8, 12, 14, 1, 6, 7, 3, 7, 8, 3, 3, 7]
功能是

import numpy as np
def ema(x, n):
    x = np.array(x)
    emaint = np.zeros(len(x))
    k = 2 / float(n + 1)

    emaint[0:n] = np.average(x[:n])

    for i in range(n, len(x)):
        emaint[i] = (x[i] * k) + (emaint[i - 1] * (1 - k))

    return emaint
如果我调用ema(test_list,5)的结果将是

我试过这个

import numpy as np
def ema_compr(x, n):
    x = np.array(x)
    emaint = np.zeros(len(x))
    k = 2 / float(n + 1)

    emaint[0:n] = np.average(x[:n])

    emaint[n:] = [(x[i] * k) + (emaint[i - 1] * (1 - k)) for i in range(n, len(x))]

    return emaint
但是,如果调用ema_compr(test_list,5),结果会有所不同:

  • 我想知道是否有可能得到一份清单
  • 列表理解的结果是否因为我要访问一个未创建的元素而有所不同
    我建议只使用Pandas中的
    ewma
    功能:

    import pandas as pd
    def ema(x, n):
        pd_x = pd.Series(x)
        pd_x[:n] = pd_x[:n].mean()
        return pd.ewma(pd_x, span=n, adjust=False).as_matrix()
    

    现在,您需要在此处指定索引:

    import numpy as np
    test_list = [3, 4, 6, 3, 8, 4, 7, 8, 12, 14, 1, 6, 7, 3, 7, 8, 3, 3, 7]
    
    def ema(x, n):
        x = np.array(x)
        emaint = np.zeros(len(x))
        k = 2 / float(n + 1)
    
        emaint[0:n] = np.average(x[:n])
    
        for i in range(n, len(x)):
            [emaint.__setitem__(i, ((x[i] * k) + (emaint[i - 1] * (1 - k)))) for i in range(n, len(x))]
    
        return emaint
    
    
    print(ema(test_list, 5))
    
    输出:

    [  4.8          4.8          4.8          4.8          4.8          4.53333333
       5.35555556   6.23703704   8.15802469  10.10534979   7.0702332
       6.7134888    6.80899253   5.53932835   6.0262189    6.68414594
       5.45609729   4.63739819   5.42493213]
    

    在列表理解中,您试图在不保存数组的情况下使用index
    emaint[i-1]
    。在列表理解中,它将在遍历整个数组时保存

    您可以像这样编写for循环来设置元素,并可以转到下一个迭代(这几乎就像您的第一个解决方案)

    范围(0,len(x))内的i的

    如果i因为循环需要保持运行状态,它不能干净地转换为列表理解,尽管存在

    因此,如果你想要“类似”列表理解,我推荐下一个最好的东西:累加器

    from itertools import accumulate
    
    def ema(x, n):
        xx = n*[sum(x[:n])/n] + x[n:]
        p, q = 2 / (n+1), (n-1) / (n+1)
        return list(accumulate(xx, lambda a, b: q*a + p*b))
    
    给出:

    ema(test_list, 5)
    # [4.8, 4.8, 4.8, 4.8, 4.8, 4.533333333333333, 5.355555555555555, 6.2370370370370365, 8.158024691358024, 10.105349794238682, 7.070233196159121, 6.713488797439414, 6.808992531626275, 5.539328354417517, 6.026218902945011, 6.684145935296673, 5.456097290197782, 4.637398193465188, 5.424932128976792]
    

    x和n的值是多少。我认为x是“测试列表”。n呢?他在问题中说ema\u compr(测试列表,5)
    的结果是什么。谢谢。你说得对,我正试图避免。。你的回答是我的出发点。当我试图提高绘图速度时,我将第一部分移出循环。我尝试了@sebastian的答案,但有点慢。我想我应该在我的问题中提到速度因素。
    for i in range(0,len(x)):
        if i<=n:
            emaint[i]=np.average(x[:n])
        else:
            emaint[i]=((x[i] * k) + (emaint[i - 1] * (1 - k)))
    
    from itertools import accumulate
    
    def ema(x, n):
        xx = n*[sum(x[:n])/n] + x[n:]
        p, q = 2 / (n+1), (n-1) / (n+1)
        return list(accumulate(xx, lambda a, b: q*a + p*b))
    
    ema(test_list, 5)
    # [4.8, 4.8, 4.8, 4.8, 4.8, 4.533333333333333, 5.355555555555555, 6.2370370370370365, 8.158024691358024, 10.105349794238682, 7.070233196159121, 6.713488797439414, 6.808992531626275, 5.539328354417517, 6.026218902945011, 6.684145935296673, 5.456097290197782, 4.637398193465188, 5.424932128976792]