for循环和创建列表列表的问题

问题描述 投票:1回答:1

我有一个名为Expect的numpy数组,它是列表列表的列表。

expected =  [
    [
        [45.0, 10.0, 10.0], 
        [110.0, 10.0, 8.0], 
        [60.0, 10.0, 5.0], 
        [170.0, 10.0, 4.0]
    ],
    [
        [-80.0, 20.0, 10.0], 
        [97.0, 15.0, 12.0], 
        [5.0, 20.0, 8.0], 
        [93.0, 10.0, 8.0], 
        [12.0, 5.0, 15.0], 
        [-88.0, 10.0, 10.0], 
        [176.0, 10.0, 8.0]
    ]
]

我想通过循环而不用硬编码,因此它适用于不同长度的列表。

当循环第一次运行时,我希望它解决这个问题:

horizontal_exp = expected[0][0][1]*expected[0][0][2]*np.cos(np.deg2rad(expected[0][0][0]))

然后下一个循环是这样的:

horizontal_exp = expected[1][1][1]*expected[1][1][2]*np.cos(np.deg2rad(expected[1][1][0]))

以及下面的循环是这样的:

horizontal_exp = expected[2][2][1]*expected[2][2][2]*np.cos(np.deg2rad(expected[2][2][0]))

依此类推,直到完成了行的不同部分。

我不明白为什么'i'从来没有工作过?

最后我希望水平期望是列表的列表

例如

expected = [ [12,21,23,34], [12,32,54,65,76,87,65] ] # these are not the values I'm just giving an example

[12,21,23,24]对应于[[45.0, 10.0, 10.0], [110.0, 10.0, 8.0], [60.0, 10.0, 5.0], [170.0, 10.0, 4.0]]

[[12,32,54,65,76,87,65]对应于[[-80.0, 20.0, 10.0], [97.0, 15.0, 12.0], [5.0, 20.0, 8.0], [93.0, 10.0, 8.0], [12.0, 5.0, 15.0], [-88.0, 10.0, 10.0], [176.0, 10.0, 8.0]]

我不确定如何执行此操作,我知道您必须在它后面附加一个for循环,但是如何将其分成列表的列表?

horizontal_expected = []
for i in list(range(len(expected[i]))):
    horizontal_exp = expected[i][i][1]*expected[i][i][2]*np.cos(np.deg2rad(expected[i][i][0]))

    horizontal_expected.append(horizontal_exp)
print(horizontal_expected)
python
1个回答
0
投票

之所以看不到所需的输出,是因为即使您具有嵌套列表expected,也只能在嵌套列表中进行迭代。您首先需要遍历外部列表,然后在内部遍历嵌套列表:

import numpy as np
expected = [ [[45.0, 10.0, 10.0], [110.0, 10.0, 8.0], [60.0, 10.0, 5.0], [170.0, 10.0, 4.0]], [[-80.0, 20.0, 10.0], [97.0, 15.0, 12.0], [5.0, 20.0, 8.0], [93.0, 10.0, 8.0], [12.0, 5.0, 15.0], [-88.0, 10.0, 10.0], [176.0, 10.0, 8.0]] ]
horizontal_expected = []
for i in range(len(expected)):
    tmp_list = []
    for j in range(len(expected[i])):
        horizontal_exp = expected[i][i][1]*expected[i][i][2]*np.cos(np.deg2rad(expected[i][i][0]))
        tmp_list.append(horizontal_exp)
    horizontal_expected.append(tmp_list)
print(horizontal_expected)

其输出是列表的列表:

>>> print(horizontal_expected)
[[70.71067811865476, 70.71067811865476, 70.71067811865476, 70.71067811865476], [-21.936481812926527, -21.936481812926527, -21.936481812926527, -21.936481812926527, -21.936481812926527, -21.936481812926527, -21.936481812926527]]

如您所见,它为输入中的每个列表保留一个值,但是该值是相同的。这是由于方程的设置方式。

您希望根据循环级别来更新索引:horizontal_exp = expected[i][j][1]*expected[i][j][2]*np.cos(np.deg2rad(expected[i][j][0]))

完整的工作代码如下:

import numpy as np
expected = [ [[45.0, 10.0, 10.0], [110.0, 10.0, 8.0], [60.0, 10.0, 5.0], [170.0, 10.0, 4.0]], [[-80.0, 20.0, 10.0], [97.0, 15.0, 12.0], [5.0, 20.0, 8.0], [93.0, 10.0, 8.0], [12.0, 5.0, 15.0], [-88.0, 10.0, 10.0], [176.0, 10.0, 8.0]] ]
horizontal_expected = []
for i in range(len(expected)):
    tmp_list = []
    for j in range(len(expected[i])):
        horizontal_exp = expected[i][j][1]*expected[i][j][2]*np.cos(np.deg2rad(expected[i][j][0]))
        tmp_list.append(horizontal_exp)
    horizontal_expected.append(tmp_list)
print(horizontal_expected)

和输出:

>>> print(horizontal_expected)
[[70.71067811865476, -27.361611466053496, 25.000000000000007, -39.39231012048832], [34.72963553338608, -21.936481812926527, 159.39115169467928, -4.186876499435507, 73.36107005503543, 3.489949670250108, -79.80512402078594]]
© www.soinside.com 2019 - 2024. All rights reserved.