Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()

前端 未结 4 621
花落未央
花落未央 2020-12-14 23:32

I was unable to find anything describing how to do this, which leads to be believe I\'m not doing this in the proper idiomatic Python way. Advice on the \'proper\' Python w

4条回答
  •  天涯浪人
    2020-12-15 00:11

    If you put your data in a collections.defaultdict you won't need to do any explicit initialization. Everything will be initialized the first time it is used.

    import numpy as np
    import collections
    n = 100
    data = collections.defaultdict(lambda: np.zeros(n))
    for i in range(1, n):
        data['g'][i] = data['d'][i - 1]
        # ...
    

提交回复
热议问题