python 如何通过计算其他列表的值来创建列表的列表

czfnxgou  于 2023-01-19  发布在  Python
关注(0)|答案(4)|浏览(130)

我有三个列表的列表和一个列表。我需要用那些数字做一个计算,得到一个列表的列表。
以下是我的清单:

E = [[352,281,230,158], [55,36,57,287], [89,65,86,25], [182,236,243,82], [214,221,344,54], [1856,2537,2443,1523]] 
I = [[0,0,0,0], [11,0,0,0], [640,0,0,35], [0,679,0,0], [0,0,1281,95], [0,5988,0,0]] 
Des = [[18,70,52,72], [55,36,57,287],[89,65,86,25], [182,236,243,82], [214,221,344,54], [1856,2537,2443,1523]] 
DesPro = [25,49,92,250,264,2130]

其他数据:

n = 6 #number of lists within lists
t = 4 #number of elements of those lists within lists`

我需要的每个元素的计算如下:

S = (E + I - Des) / DesPro

我试过:

for i in range(n):
  for t in range(T):
    S = (E[i][t] + I[i][t] - Des[i][t]) / DesPro[i]
    print(S)

我期望:

[[13.36, 8.44, 7.12, 3.44], [0.22448979591836735, 0.0, 0.0, 0.0], [6.956521739130435, 0.0, 0.0, 0.3804347826086957], [0.0, 2.716, 0.0, 0.0], [0.0, 0.0, 4.8522727272727275, 0.35984848484848486], [0.0, 2.8112676056338026, 0.0, 0.0]]

但我得到了:

13.36
8.44
7.12
3.44
0.22448979591836735
0.0
0.0
0.0
6.956521739130435
0.0
0.0
0.3804347826086957
0.0
2.716
0.0
0.0
0.0
0.0
4.8522727272727275
0.35984848484848486
0.0
2.8112676056338026
0.0
0.0
kyxcudwk

kyxcudwk1#

Numpy可以帮你算!

import numpy as np

E = np.array()[352,281,230,158], [55,36,57,287], [89,65,86,25], [182,236,243,82], [214,221,344,54], [1856,2537,2443,1523]])
I = np.array([[0,0,0,0], [11,0,0,0], [640,0,0,35], [0,679,0,0], [0,0,1281,95], [0,5988,0,0]]) 
Des = np.array([[18,70,52,72], [55,36,57,287],[89,65,86,25], [182,236,243,82], [214,221,344,54], [1856,2537,2443,1523]]) 
DesPro = np.array([25,49,92,250,264,2130])

(E + I - Des) / DesPro[:,None]
# array([[13.36      ,  8.44      ,  7.12      ,  3.44      ],
#        [ 0.2244898 ,  0.        ,  0.        ,  0.        ],
#        [ 6.95652174,  0.        ,  0.        ,  0.38043478],
#        [ 0.        ,  2.716     ,  0.        ,  0.        ],
#        [ 0.        ,  0.        ,  4.85227273,  0.35984848],
#        [ 0.        ,  2.81126761,  0.        ,  0.        ]])

DesPro[:,None]DesPro[:, np.newaxis]是匹配(广播)形状所必需的。read more on boradcasting (numpy docs)

5anewei6

5anewei62#

E = [[352,281,230,158], [55,36,57,287], [89,65,86,25], [182,236,243,82], [214,221,344,54], [1856,2537,2443,1523]] 
I = [[0,0,0,0], [11,0,0,0], [640,0,0,35], [0,679,0,0], [0,0,1281,95], [0,5988,0,0]] 
Des = [[18,70,52,72], [55,36,57,287],[89,65,86,25], [182,236,243,82], [214,221,344,54], [1856,2537,2443,1523]] 
DesPro = [25,49,92,250,264,2130]
n = 6 #number of lists within lists
T = 4 #number of elements of those lists within lists`
results = []
for i in range(n):
      resultsSubList = []
      for t in range(T):
        S = (E[i][t] + I[i][t] - Des[i][t]) / DesPro[i]
        resultsSubList.append(S)
      results.append(resultsSubList)
print(results)

问题是你在做数学计算,但你只是在记录变量,就像你用for循环解构输入数组一样,你需要把它重新组合在一起。
你最初做的很好,因为它创建了正确的数字串,你只需要确保它把这些数字存储在数组中,而不是立即输出它们。

qlvxas9a

qlvxas9a3#

使用列表解析来组织数据

[
    [
        (E[i][t] + I[i][t] - Des[i][t]) / DesPro[i]
        for t in range(T)
    ]
    for i in range(n)
]
j0pj023g

j0pj023g4#

可以使用zip匹配每个列表中的元素,并删除索引和列表解析以执行嵌套计算:

E = [[352,281,230,158], [55,36,57,287], [89,65,86,25], [182,236,243,82], [214,221,344,54], [1856,2537,2443,1523]] 
I = [[0,0,0,0], [11,0,0,0], [640,0,0,35], [0,679,0,0], [0,0,1281,95], [0,5988,0,0]] 
Des = [[18,70,52,72], [55,36,57,287],[89,65,86,25], [182,236,243,82], [214,221,344,54], [1856,2537,2443,1523]] 
DesPro = [25,49,92,250,264,2130]

S = [[(e + i - des) / despro for e, i, des in zip(elist, ilist, dlist)]
     for elist, ilist, dlist, despro in zip(E, I, Des, DesPro)]

print(S)

输出:

[[13.36, 8.44, 7.12, 3.44], [0.22448979591836735, 0.0, 0.0, 0.0], [6.956521739130435, 0.0, 0.0, 0.3804347826086957], [0.0, 2.716, 0.0, 0.0], [0.0, 0.0, 4.8522727272727275, 0.35984848484848486], [0.0, 2.8112676056338026, 0.0, 0.0]]

相关问题