for循环和创建lis列表时出现问题

2024-05-17 19:06:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为expected的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]))

以此类推,直到它完成了行的不同部分。你知道吗

我不明白为什么“我”从来没用过??你知道吗

最后我希望横向的预期是一个列表的列表

例如

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)

Tags: thenumpy编码列表fornpnot数组
1条回答
网友
1楼 · 发布于 2024-05-17 19:06:08

看不到所需输出的原因是,即使有嵌套列表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]]

相关问题 更多 >