在我的例子中,我有两个循环。一个嵌套在另一个中。有没有办法可以从外部索引开始内循环。这是伪代码。
arrayOfWords = ["one","two","five"]
arrayOfWords2 = ["one","two","three","four","five"]
tottalWordlist = []
for index, jString in enumerate(arrayOfWords):
gWord = arrayOfWords[index]
indexClone = index
arrayOfWords2Count = range(len(arrayOfWords2)-1)
for indexClone in arrayOfWords2Count:
if gWord == arrayOfWords2[indexClone]:
tottalWordlist.append(gWord)
break
答案 0 :(得分:4)
你几乎就在那里。您只需要将起始索引添加到range
对象。
arrayOfWords2Count = range(indexClone, len(arrayOfWords2))
for i in arrayOfWords2Count:
if gWord == arrayOfWords2[i]:
此外,您不需要从端点中减去一个,因为该端是独占的。
答案 1 :(得分:2)
在我的理解中,您正在寻找内部列表中的外部列表元素是否以外部元素的索引开头。
arrayOfWords = ["one","two","five"]
arrayOfWords2 = ["one","two","three","four","five"]
print [ item for i,item in enumerate(arrayOfWords) if item in arrayOfWords2[i:] ]
['one', 'two', 'five']