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

Python for循环和创建列表列表的问题,python,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

我有一个名为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]))
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)
下面的循环是这样的:

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]))
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)
依此类推,直到它完成了行的不同部分

我不明白为什么“我”从来没有工作过

最后,我希望水平面是一个列表中的一个列表

e、 g

其中
[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)

您看不到所需输出的原因是,即使您有嵌套列表
预期值
,但您只在嵌套列表中进行迭代。首先需要遍历外部列表,然后在内部遍历嵌套列表:

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])

完整的工作代码如下所示:

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]))
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]]

您还可以发布现有代码的结果吗?我想我看到了这个问题,但只是想在发布解决方案之前确认一下。@kingkupps它出现了一个错误“索引2超出了轴0的界限,大小为2”@LucyBrown对于未来,我同意kingkupps的观点,也可以发布您得到的错误,以便第一眼就更容易识别问题。另外,StackOverflow的一些好的灵魂为你的帖子提供了很好的格式,这使你更有可能得到一个好的答案。