【问题标题】:Python 3: How to remove matches from 2 lists (full of duplicates) without using set nor collections nor importing anything?Python 3:如何在不使用集合或集合或导入任何内容的情况下从 2 个列表(充满重复项)中删除匹配项?
【发布时间】:2019-03-11 06:08:24
【问题描述】:

请帮忙。

>>> a = [7, 2, 2, 2, 3, 2]
>>> b = [2, 2, 2, 3, 3, 5]
Expected result:    [2, 7, 3, 5]

有关“图片展示”,请参阅 https://www.w3resource.com/python-exercises/python-basic-exercise-32.php

不允许设置。 不允许收藏。 因为我正在尝试学习循环。

我尝试过与 Intersection of two lists including duplicates? 因为该线程试图做与我试图实现的相反的事情。

还请注意,此问题与以下问题不同: Python removing items from a list that exist in another list but keeping duplicates that aren't in that intersect 也不 Python intersection of two lists keeping duplicates 也不 Python: intersection of 2 lists keeping duplicates from both lists

非常感谢。

【问题讨论】:

    标签: python-3.x


    【解决方案1】:
    c = list()
    for x in a+b:
        if x not in c:
            c.append(x)
    

    【讨论】:

    • 感谢您的快速响应,mujjiga 一百万。简短而甜蜜。也许您还可以简化stackoverflow.com/questions/37645053/… 中的一些解决方案,它们太复杂了,我无法理解。感谢您的帮助。
    • @goughgough np :) 完成添加 :)
    • 谢谢一百万。我为您非常简单易懂的解决方案添加了评论。毫无疑问,这是最优雅、最容易遵循的解决方案。 mujjiga 你是个天才。感谢您的帮助。
    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2021-05-21
    • 2017-10-25
    • 2015-02-20
    • 2019-04-03
    • 2015-10-05
    相关资源
    最近更新 更多