Python 如何从嵌套for循环中的同一元素开始?

Python 如何从嵌套for循环中的同一元素开始?,python,Python,我很难理解如何使嵌套for循环从“相同”元素开始 以下是我的部分代码: for elem1 in sentence2: for elem2 in bigram_list: if (elem1 == vocab_list[int(elem2[0]) - 1]): file.write("elem1: " + elem1 + "\n") file.write("vocab: " + vocab_list[int(elem2[0]

我很难理解如何使嵌套for循环从“相同”元素开始

以下是我的部分代码:

for elem1 in sentence2:
    for elem2 in bigram_list:
        if (elem1 == vocab_list[int(elem2[0]) - 1]):
            file.write("elem1: " + elem1 + "\n")
            file.write("vocab: " + vocab_list[int(elem2[0]) - 1] + "\n")
            index3 = index3 + 1
            for (sameElem2 in bigram_list):
                if (sentence2[index3] == vocab_list[int(sameElem2[1]) - 1]
问题是,bigram_列表中的每个elem2都包含索引elem2[0],elem2[1],elem2[2]

我试图检查bigram_列表中元素[0]处的一个元素是否等于我的句子2_列表中的另一个元素,如果等于,我希望能够开始循环下一个索引,即elem2[1],但仍然位于bigram_列表中的同一个元素,并检查它是否等于句子2_列表中的下一个元素


但这就是我被困的地方。我想向前走,而不是向后走:)

如果我理解正确,您希望在前进的过程中更改迭代的起点。如果是这样,您可以通过一些简单的切片来实现这一点:

for i, elem1 in enumerate(bigram_list):
    for elem2 in bigram_list[i:]:
        # Do something

作为一点解释,
enumerate
是一个python函数,它只返回当前使用的索引和单个元组中的元素,这意味着您可以像上面那样访问这两个元素。

如果我理解正确,您希望在执行时更改迭代的起点。如果是这样,您可以通过一些简单的切片来实现这一点:

for i, elem1 in enumerate(bigram_list):
    for elem2 in bigram_list[i:]:
        # Do something

作为一点解释,
enumerate
是一个python函数,它只返回当前使用的索引和单个元组中的元素,这意味着您可以像上面那样访问这两个元素。

如果我理解正确,您希望在执行时更改迭代的起点。如果是这样,您可以通过一些简单的切片来实现这一点:

for i, elem1 in enumerate(bigram_list):
    for elem2 in bigram_list[i:]:
        # Do something

作为一点解释,
enumerate
是一个python函数,它只返回当前使用的索引和单个元组中的元素,这意味着您可以像上面那样访问这两个元素。

如果我理解正确,您希望在执行时更改迭代的起点。如果是这样,您可以通过一些简单的切片来实现这一点:

for i, elem1 in enumerate(bigram_list):
    for elem2 in bigram_list[i:]:
        # Do something

作为一点解释,
enumerate
是一个python函数,它只返回当前使用的索引和单个元组中的元素,这意味着您可以像上面那样访问这两个元素。

您可以使用索引:

for elem1 in sentence2:
    for i in range(0,len(bigram_list)):
        elem2 = int(bigram_list[i])-1
        if (elem1 == vocab_list[elem2]):
            file.write("elem1: " + elem1 + "\n")
            file.write("vocab: " + vocab_list[elem2] + "\n")
            if i+1 < len(bigram_list):
                nextElem2 = int(bigram_list[i+1])-1
                index3 = index3 + 1 # Make sure that you want to increment 'index3' here and not inside the 'if'
                if sentence2[index3] == vocab_list[nextElem2]:
                    ...
对于第2句中的elem1:
对于范围(0,len(bigram_列表))中的i:
elem2=int(bigram_list[i])-1
如果(elem1==vocab_列表[elem2]):
file.write(“elem1:+elem1+”\n)
file.write(“vocab:+vocab\u list[elem2]+“\n”)
如果i+1
您可以改用索引:

for elem1 in sentence2:
    for i in range(0,len(bigram_list)):
        elem2 = int(bigram_list[i])-1
        if (elem1 == vocab_list[elem2]):
            file.write("elem1: " + elem1 + "\n")
            file.write("vocab: " + vocab_list[elem2] + "\n")
            if i+1 < len(bigram_list):
                nextElem2 = int(bigram_list[i+1])-1
                index3 = index3 + 1 # Make sure that you want to increment 'index3' here and not inside the 'if'
                if sentence2[index3] == vocab_list[nextElem2]:
                    ...
对于第2句中的elem1:
对于范围(0,len(bigram_列表))中的i:
elem2=int(bigram_list[i])-1
如果(elem1==vocab_列表[elem2]):
file.write(“elem1:+elem1+”\n)
file.write(“vocab:+vocab\u list[elem2]+“\n”)
如果i+1
您可以改用索引:

for elem1 in sentence2:
    for i in range(0,len(bigram_list)):
        elem2 = int(bigram_list[i])-1
        if (elem1 == vocab_list[elem2]):
            file.write("elem1: " + elem1 + "\n")
            file.write("vocab: " + vocab_list[elem2] + "\n")
            if i+1 < len(bigram_list):
                nextElem2 = int(bigram_list[i+1])-1
                index3 = index3 + 1 # Make sure that you want to increment 'index3' here and not inside the 'if'
                if sentence2[index3] == vocab_list[nextElem2]:
                    ...
对于第2句中的elem1:
对于范围(0,len(bigram_列表))中的i:
elem2=int(bigram_list[i])-1
如果(elem1==vocab_列表[elem2]):
file.write(“elem1:+elem1+”\n)
file.write(“vocab:+vocab\u list[elem2]+“\n”)
如果i+1
您可以改用索引:

for elem1 in sentence2:
    for i in range(0,len(bigram_list)):
        elem2 = int(bigram_list[i])-1
        if (elem1 == vocab_list[elem2]):
            file.write("elem1: " + elem1 + "\n")
            file.write("vocab: " + vocab_list[elem2] + "\n")
            if i+1 < len(bigram_list):
                nextElem2 = int(bigram_list[i+1])-1
                index3 = index3 + 1 # Make sure that you want to increment 'index3' here and not inside the 'if'
                if sentence2[index3] == vocab_list[nextElem2]:
                    ...
对于第2句中的elem1:
对于范围(0,len(bigram_列表))中的i:
elem2=int(bigram_list[i])-1
如果(elem1==vocab_列表[elem2]):
file.write(“elem1:+elem1+”\n)
file.write(“vocab:+vocab\u list[elem2]+“\n”)
如果i+1
在这种情况下,切片比索引更干净、更高效。我不能将I用作索引,因为我需要能够访问我的二进制内存中每个元素的索引_list@TokugawaIeysu您仍然可以使用索引,但可以链接它们,例如
bigram\u list[i][1]
,但这确实会使事情变得混乱和不那么直观。你仍然可以使用
currElem2[index]
,就像你以前使用
elem2[index]
(更新了答案以使其更清晰)…你好,巴拉克,我更新了我的问题,我问的问题不对:)在这种情况下,切片比索引更干净、更高效。我不能将I用作索引,因为我需要能够访问bigram中每个元素的索引_list@TokugawaIeysu您仍然可以使用索引,但可以链接它们,例如
bigram\u list[i][1]
,但这确实会使事情变得混乱和不那么直观。你仍然可以使用
currElem2[index]
,就像你以前使用
elem2[index]
(更新了答案以使其更清晰)…你好,巴拉克,我更新了我的问题,我问的问题不对:)在这种情况下,切片比索引更干净、更高效。我不能将I用作索引,因为我需要能够访问bigram中每个元素的索引_list@TokugawaIeysu您仍然可以使用索引,但可以链接它们,例如
bigram\u list[i][1]
,但这确实会使事情变得杂乱无章,不那么直观