【问题标题】:How to determine if a list with duplicates is contained in another list also with duplicates如何确定具有重复项的列表是否包含在另一个也具有重复项的列表中
【发布时间】:2021-08-22 17:15:03
【问题描述】:

我想比较 2 个列表以了解其中一个是否包含另一个的某些元素。这是一个例子:

contents = ['blue', 'blue', 'blue', 'red', 'red', 'green']
expected = ['blue', 'blue', 'red']

if expected in contents:
    return True
else:
    return False

我希望该代码返回 True。 但例如,如果我使用:

expected = ['red', 'green', 'green']

这应该返回 False,因为内容上只有一个绿色。

【问题讨论】:

    标签: python list


    【解决方案1】:
    def colors():
        for x in expected:
            if x in contents:
                contents.remove(x)
                if x in contents:
                    good.append(x)
        if len(good) == len(expected):
            return True
        else:
            return False
    print(colors())
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    【解决方案2】:

    我认为您可以在这里使用collections.Counter(灵感来自this answer)。获取expectedcontents 中唯一元素的计数,然后遍历expected 中的项目以确保contents 中的计数至少相同。

    from collections import Counter
    
    contents = ['blue', 'blue', 'blue', 'red', 'red', 'green']
    expected = ['blue', 'blue', 'red']
    
    a = Counter(contents)
    b = Counter(expected)
    
    print(all([a[k] >= b[k] for k in b.keys()]))
    # True
    
    c = Counter(['red', 'green', 'green'])
    print(all([a[k] >= c[k] for k in c.keys()])) # c instead of b
    # False
    

    函数形式:

    def list_inside(contents, expected):
        contents = Counter(contents)
        expected = Counter(expected)
        return all([contents[k] >= expected[k] for k in expected.keys()])
    

    【讨论】:

    • 看起来不错,但我宁愿不导入任何模块!
    • @PolPorta collections 是标准库的一部分,所以 Python 自动自带;如果对您的代码有帮助,我会说鼓励使用这些标准模块! IE。不要重新实现已经可用的东西
    【解决方案3】:

    这应该可以工作,即使是重复的:

    def contains(haystack, needle):
        haystack = haystack.copy()
        for item in needle:
            if item not in haystack:
                return False
            haystack.remove(item)
        return True
    
    
    contents = ['blue', 'blue', 'blue', 'red', 'red', 'green']
    print(contains(contents, ['blue', 'blue', 'red']))
    # True
    print(contains(contents, ['red', 'green', 'green']))
    # False
    

    【讨论】:

    • 会尝试,但我正在尝试不导入任何模块!还是谢谢!
    • 这里不需要输入。你可以删除它。
    • 那太好了!我试过了,它的工作,非常感谢你!而且我实际上正试图将它放在一个函数中,没有这个函数有没有办法做到这一点?所以我不必在另一个函数中使用一个函数?
    • 如果我尝试检索超过 3 种颜色,它就不起作用,知道为什么吗?
    • 你能举个例子吗? print(contains(contents, ['blue', 'blue', 'red', 'green'])) 也适用于我 (= True)
    【解决方案4】:

    这是一个小代码。

    contents = ['blue', 'blue', 'blue', 'red', 'red', 'green']
    expected = ['blue', 'blue', 'red']
    new_colours =[x for x in expected if x in contents] #=== Check if the value in contents
    print(new_colours)
    

    输出:

    ['blue', 'blue', 'red']
    

    这里,我改expectedlist

    expected=['blue','blue','yellow']
    

    输出:

    ['blue', 'blue']
    

    【讨论】:

    • 好的,但这会为我创建一个新列表,如果其中至少有一个元素,它总是正确的,我需要检查它们是否都在另一个列表中,并且return True,但我想我会接受你的想法,然后计算输出列表的长度是否与预期的长度相同,非常感谢!
    • 没问题@PolPorta,
    • 还有一个问题,我可以有 8 个预期的蓝色球,它会返回 8 次“蓝色”,而只有 3 个!
    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2022-01-04
    • 2018-12-22
    • 2019-03-07
    • 2015-02-25
    • 1970-01-01
    相关资源
    最近更新 更多