gpt4 book ai didi

如果此子列表中有特定元素,Python 会删除列表中的子列表

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:11 24 4
gpt4 key购买 nike

例如

list_of_specific_element = [2,13]
list = [[1, 0], [2, 1], [2, 3], [13, 12], [13, 14], [15, 13]]

我希望从列表中删除包含特定元素列表中的任何值的子列表。
因此元素 [2,1],[2,3],[13,12],[13,14] 应该从列表中删除。
最终输出列表应该是[[1,0],[15,13]]

最佳答案

listy=[elem for elem in listy if (elem[0] not in list_of_specific_element) and (elem[1] not in list_of_specific_element)]

使用单行列表理解

关于如果此子列表中有特定元素,Python 会删除列表中的子列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44221836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com