gpt4 book ai didi

python 3 : How can I check if two lists match even if elements are not in order

转载 作者:行者123 更新时间:2023-11-28 21:43:22 26 4
gpt4 key购买 nike

我有两个列表:

original_list= [1,2,3,4,5]

new_list = [[1,7,3,4], [1,2,4,5,3]]

除了使用 sort 和 list in list 之外,我如何比较 original_list 值在 new_list 中?

最佳答案

您想对 new_list 中的每个 list 进行排序,而不是 new_list 本身。例如使用 map 试试这个:

print(sorted(original_list) in map(sorted, new_list))

或者如果您只关心相同元素的存在而不考虑它们的数量:

print(set(original_list) in map(set, new_list))

关于 python 3 : How can I check if two lists match even if elements are not in order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42320074/

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