我希望这个函数遍历两个列表并返回两者中相同的对象

时间:2016-09-28 11:26:34

标签: python list

这些是清单;请注意第二个列表是如何由首先需要拆分的对象组成的。

gaps = ['__1__', '__2__', '__3__']
questions = ['Bruce Wayne is __1__', 'Clark Kent is __2__', 'Barry Allen is __3__']

这是代码。它似乎仅适用于第一个(第0个)对象,芽就是它 - 它甚至不会循环。

def is_gap(question, gap):
place = 0
while (place < 3):
    question[place] = question[place].split(' ')
    for index in gaps:
            if index in question[place]:
                return index
            else:
                return None
place += 1
print is_gap(questions, gaps)

1 个答案:

答案 0 :(得分:0)

Python一旦命中return语句就会停止执行。如果你在循环内部返回,它将在一次迭代后停止运行,当它遇到第一个return语句时。