Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在同一列表中有两个for循环,但第二个从第一个循环的下一个值开始?_Python_List_Nested For Loop - Fatal编程技术网

Python 如何在同一列表中有两个for循环,但第二个从第一个循环的下一个值开始?

Python 如何在同一列表中有两个for循环,但第二个从第一个循环的下一个值开始?,python,list,nested-for-loop,Python,List,Nested For Loop,我正试图编写一个代码,其中我有一个向量列表,我必须找到每个向量与其余向量之间的角度(我正在研究mediapipe的手形地标)。 到目前为止,我的代码是: vectors = [thumb_cmc_vec, thumb_mcp_vec, thumb_ip_vec, thumb_tip_vec, index_mcp_vec, index_pip_vec, index_dip_vec, index_tip_vec, middle_mcp_vec,

我正试图编写一个代码,其中我有一个向量列表,我必须找到每个向量与其余向量之间的角度(我正在研究mediapipe的手形地标)。 到目前为止,我的代码是:

vectors = [thumb_cmc_vec, thumb_mcp_vec, thumb_ip_vec, thumb_tip_vec, index_mcp_vec, index_pip_vec,
                           index_dip_vec, index_tip_vec, middle_mcp_vec, middle_pip_vec, middle_dip_vec, middle_tip_vec,
                           ring_mcp_vec, ring_pip_vec, ring_dip_vec, ring_tip_vec, pinky_mcp_vec, pinky_pip_vec,
                           pinky_dip_vec, pinky_tip_vec]

                for vector in vectors:
                    next_vector = vector + 1
                    print(vector)
                    for next_vector in vectors:
                        print(next_vector)
                        M = (np.linalg.norm(vector) * np.linalg.norm(next_vector))
                        ES = np.dot(vector, next_vector)
                        th = math.acos(ES / M)
                        list.append(th)
                        print(list)
其中M=当前向量集的范数的乘积,ES= 向量和th的标量积=向量的角度。 我的问题是变量
next\u vector
总是从列表的第一个向量开始for循环,即使我希望它从上一个循环的下一个向量开始,以避免重复结果。另外,当两个循环都在第三个向量(thumb_ip_vec)上时,我得到了这个错误 th=math.acos(ES/M)
ValueError:数学域错误
。有什么办法解决这个问题吗?谢谢大家!

我认为您可以迭代列表索引(使用
range(len(vectors)-1)
)并通过索引访问元素,而不是循环访问每个元素

for i in range(len(vectors) - 1):
    # Iterate from 0 to len(vectors) -1
    vector = vectors[i]
    for j in range(i + 1, len(vectors)):
        # Iterate from index i + 1 to len(vectors)
        next_vector = vectors[j]
        M = (np.linalg.norm(vector) * np.linalg.norm(next_vector))
        ES = np.dot(vector, next_vector)
        th = math.acos(ES / M)
        list.append(th)
        print(list)

我不确定我理解你的问题,但是考虑使用范围。 范围允许您进行迭代,但不只是调用确切的值,而是调用它的地址。 这意味着您可以操纵该索引来访问相邻的值

for i in range(len(iterables)-1):
    ii = i+1
    initial_value = iterables[i]
    next_value = iterables[ii]

    for ii in range(len(iterables)):
        # do_rest_of_code
                               
有点像邮递员,你可以在不知道邻居地址的情况下联系到邻居。

上面的结构通常可以正常工作,但您需要调整它以满足您的需要。

这里的有效解决方案是使用
向量:

from itertools import combinations  # At top of file

for vector, next_vector in itertools.combinations(vectors, 2):
    M = (np.linalg.norm(vector) * np.linalg.norm(next_vector))
    ES = np.dot(vector, next_vector)
    th = math.acos(ES / M)
    list.append(th)

它比在索引和索引上循环快得多,降低了循环嵌套的级别,并使您的操作更加清晰(使用每个唯一的输入配对)。

非常感谢,它工作得非常完美!