从另一个列表中删除一个列表的元素

发布于 2025-02-06 10:49:23 字数 163 浏览 2 评论 0原文

是否有一种Pythonic将元素从一个列表中删除到另一个列表的方法? (不是删除所有重复项)

例如,给定[1、2、2、3、3、3](原始列表)和[1、2、3](要删除的元素)。它将返回[2,3,3] 我们可以假设给定的两个列表始终有效。 “要删除”列表中的元素将在原始列表中。

谢谢!

Is there a pythonic way to remove elements from one list to another list? (Not removing all duplicates)

For example, given [1, 2, 2, 3, 3, 3] (original list) and [1, 2, 3] (elements to be removed). It will return [2, 3, 3]
We can assume that the two given lists are always valid. Elements in the "to be removed" list will be in the original list.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

琉璃梦幻 2025-02-13 10:49:24

这个工作


original  = [1, 2, 2, 3, 3, 3] 
remove = [1, 2, 3] 

for i, x in enumerate(original):
    if x in remove:
        original.pop(i)

print(original)

This shoud work


original  = [1, 2, 2, 3, 3, 3] 
remove = [1, 2, 3] 

for i, x in enumerate(original):
    if x in remove:
        original.pop(i)

print(original)
夏日浅笑〃 2025-02-13 10:49:23

我将使用要删除的元素的计数器。

因此,这样的

from collections import Counter 

data = [1, 2, 2, 3, 3, 3]
to_be_removed = [1, 2, 3, 3] # notice the extra 3

counts = Counter(to_be_removed)

new_data = []
for x in data:
    if counts[x]:
        counts[x] -= 1
    else:
        new_data.append(x)

print(new_data)

解决方案是线性时间 /空间。如果您实际上需要修改列表(IMO代码 - Smell,IMO),则需要二次时间

注意,请考虑您是否实际上只想要一个多键 - 即,您可以一直在使用计数器,请考虑:

>>> Counter(data) - Counter(to_be_removed)
Counter({2: 1, 3: 1})

I would use a counter of the elements to be removed.

So, something like

from collections import Counter 

data = [1, 2, 2, 3, 3, 3]
to_be_removed = [1, 2, 3, 3] # notice the extra 3

counts = Counter(to_be_removed)

new_data = []
for x in data:
    if counts[x]:
        counts[x] -= 1
    else:
        new_data.append(x)

print(new_data)

This solution is linear time / space. If you actually need to modify the list (which is code-smell, IMO), you would require quadratic time

Note, consider if you actually just want a multiset - i.e. you could just be working with counters all along, consider:

>>> Counter(data) - Counter(to_be_removed)
Counter({2: 1, 3: 1})
梦归所梦 2025-02-13 10:49:23

我不知道您对此操作解决了什么问题,因此不确定是否适用,但是如果您可以从原始列表中删除的元素(例如,从原始列表中获取随机子序列,然后减去它)然后,Pythonic,更有效的方法(如果您进行大量操作)将是使用可用对象的列表,而不是整数列表。这样一来,您就可以简单地做set1 -set2来实现目标:

# Create your own element class
class MyElement:
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return str(self.value)

    # support for less than operator needed for sorting
    def __lt__(self, other):
        return self.value < other.value


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

# Transform list of integers to list of my own MyElement objects
original_list = [MyElement(element) for element in original_list]

# Create subsequence of the original list
to_be_removed = original_list[1:4]

print(original_list)  # [1, 2, 2, 3, 3, 3]
print(to_be_removed)  # [2, 2, 3]

# Subtract the sets
result = set(original_list) - set(to_be_removed)

# Print sorted result
print(sorted(result))  # [1, 3, 3]

它不会保留原始订单!但是您可以在最后对其进行排序。

I don't know what problem are you solving with this operation so not sure if this is applicable, but if you can take the elements you want to remove from the original list (for example get random subsequence from the original list and then subtract it) then pythonic and more efficient way (if you do a lot of this operation) would be to use list of hashable objects instead of a list of integers. That way you can do simply set1 - set2 to achieve the goal:

# Create your own element class
class MyElement:
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return str(self.value)

    # support for less than operator needed for sorting
    def __lt__(self, other):
        return self.value < other.value


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

# Transform list of integers to list of my own MyElement objects
original_list = [MyElement(element) for element in original_list]

# Create subsequence of the original list
to_be_removed = original_list[1:4]

print(original_list)  # [1, 2, 2, 3, 3, 3]
print(to_be_removed)  # [2, 2, 3]

# Subtract the sets
result = set(original_list) - set(to_be_removed)

# Print sorted result
print(sorted(result))  # [1, 3, 3]

It won't preserve original order! But you can sort it at the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文