执行删除操作后的列表是:在python中0x000001D5A0952FA0>错误处<过滤对象


# Python 3 code to demonstrate
# to remove elements present in other list
# using filter() + lambda
# initializing list
list1 = [1, 3, 4, 6, 7]
# initializing remove list
list2 = [3, 6]
# printing original list
print("The original list is : " + str(list1))
# printing remove list
print("The original list is : " + str(list2))
# using filter() + lambda to perform task
res = filter(lambda i: i not in list1, list2)
# printing result
print("The list after performing remove operation is : " + str(res))
Output-:
The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : <filter object at 0x000001D5A0952FA0>

Expected output-:
The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : [1, 4, 7]

这里有什么问题?为什么我会犯那个错误。我真的无法理解这个问题。我在一个名为极客的网站上找到了这个,并试着运行它

https://www.geeksforgeeks.org/python-remove-all-values-from-a-list-present-in-other-list/

这两个列表位于错误的位置,为了显示结果,您可以将过滤器对象转换为列表

res = list(filter(lambda i: i not in list2, list1))
print("The list after performing remove operation is : " + str(res))
OUTPUTS: The list after performing remove operation is : [1, 4, 7]

您需要更改过滤函数参数。你的代码应该喜欢这个

list1 = [1, 3, 4, 6, 7]
# initializing remove list
list2 = [3, 6]
# printing original list
print("The original list is : " + str(list1))
# printing remove list
print("The original list is : " + str(list2))
# using filter() + lambda to perform task
res = list(filter(lambda i: i not in list2, list1))
print(list1)
print(list2)
print(res)

该代码的输出将类似于

The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
[1, 3, 4, 6, 7]
[3, 6]
[1, 4, 7]

相关内容

  • 没有找到相关文章

最新更新