How to sum the elements of N lists?

前端 未结 3 1882
悲&欢浪女
悲&欢浪女 2020-12-17 03:25

can anybody please give me a hint how to aggregate (sum up) specific elements of multiple lists?

For example I have 20 lists with a similar name pattern, each represe

3条回答
  •  猫巷女王i
    2020-12-17 04:04

    You can create a list of lists, and then use numpy arrays:

    import numpy as np
    c_agent_0 = [10.0, 11.0, 12.0]
    c_agent_1 = [13.0, 14.0, 15.0]
    c_agent_2 = [16.0, 17.0, 18.0]
    mylist = [c_agent_0, c_agent_1, c_agent_2]
    mylist = sum(map(np.array, mylist))
    print mylist[0]
    

    Note that a list comprehension is most likely faster; this is just a solution with numpy :).

提交回复
热议问题