我最近开始使用Python编程,并且编写了一个简单的函数,它需要两个列表并返回一个新的列表,这两个列表重合。但是,当我运行代码时,它返回一个空列表。请帮助:python程序返回空列表
x = [1, 2, 3]
y = [4, 5, 6]
def reunion(list_of_numbers1,list_of_numbers2):
union_list = list()
for i in range(0,len(list_of_numbers1)):
if list_of_numbers1[i] in list_of_numbers2 is True:
union_list.append(i)
del list_of_numbers1[i]
del list_of_numbers2[i]
return union_list
z = reunion(x,y)
print(z)
请在发布Python代码时准确再现您的缩进。严重缩减的Python代码是无稽之谈。 – khelwood
这段代码显然会返回一个空列表,因为在这两个列表之间没有共享元素(这个代码还有其他一些错误)。 – UnholySheep
此代码的预期输出是什么?因为你是如果语句没有得到处理,因为'i'不在'list_of_numbers2'中,所以没有任何东西会被追加。 – RoadRunner