如何使用 set 维护列表的顺序?

发布于 2024-09-15 12:53:38 字数 216 浏览 11 评论 0原文

In [1]: l1 = ['a',2,3,0,9.0,0,2,6,'b','a']

In [2]: l2 = list(set(l1))

In [3]: l2
Out[3]: ['a', 0, 2, 3, 6, 9.0, 'b']

在这里您可以看到列表 l2 的顺序与原始 l1 的顺序不同,我需要从列表中删除重复的元素而不更改列表元素的顺序/顺序......

In [1]: l1 = ['a',2,3,0,9.0,0,2,6,'b','a']

In [2]: l2 = list(set(l1))

In [3]: l2
Out[3]: ['a', 0, 2, 3, 6, 9.0, 'b']

Here you can see the the list l2 is falling with different sequence then the original l1, I need to remove the duplicate elements from my list without changing the sequence/order of the list elements....

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

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

发布评论

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

评论(3

仙女 2024-09-22 12:53:38

如果你不关心效率,这是 O(n*m)

>>> sorted(set(l1), key=l1.index)
['a', 2, 3, 0, 9.0, 6, 'b']

使用中间字典更复杂,但是是 O(n+m*logm)

其中 n 是 l1 中的元素数量,m 是唯一的数量l1 中的元素

>>> l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
>>> d1=dict((k,v) for v,k in enumerate(reversed(l1)))
>>> sorted(d1, key=d1.get, reverse=True)
['a', 2, 3, 0, 9.0, 6, 'b']

在 Python3.1 中你有 OrderedDict 所以这很容易

>>> l1 = ['a',2,3,0,9.0,0,2,6,'b','a'] 
>>> list(OrderedDict.fromkeys(l1))
['a', 2, 3, 0, 9.0, 6, 'b']

If you are not concerned with efficiency, this is O(n*m)

>>> sorted(set(l1), key=l1.index)
['a', 2, 3, 0, 9.0, 6, 'b']

Using an intermediate dict is more complicated, but is O(n+m*logm)

where n is the number of elements in l1 and m is the number of unique elements in l1

>>> l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
>>> d1=dict((k,v) for v,k in enumerate(reversed(l1)))
>>> sorted(d1, key=d1.get, reverse=True)
['a', 2, 3, 0, 9.0, 6, 'b']

In Python3.1 you have OrderedDict so it's very easy

>>> l1 = ['a',2,3,0,9.0,0,2,6,'b','a'] 
>>> list(OrderedDict.fromkeys(l1))
['a', 2, 3, 0, 9.0, 6, 'b']
清风不识月 2024-09-22 12:53:38

您可以通过定义如下函数来解决它:

def dedupe(items):
    seen = set()
    for item in items:
        if item not in seen:
            yield item
            seen.add(item)

要使用它:

>>> l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
>>> l2 = list(dedupe(l1))
>>> l2
['a', 2, 3, 0, 9.0, 6, 'b']

You can solve it by defining a function like this:

def dedupe(items):
    seen = set()
    for item in items:
        if item not in seen:
            yield item
            seen.add(item)

To use it:

>>> l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
>>> l2 = list(dedupe(l1))
>>> l2
['a', 2, 3, 0, 9.0, 6, 'b']
不如归去 2024-09-22 12:53:38

这是我的头脑中的想法(使用字典):

l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
l2 = []
s = {}
for i in l1:
    if not i in s:
        l2.append(i)
        s[i] = None

# l2 contains ['a', 2, 3, 0, 9.0, 6, 'b', 'a']

编辑:使用集合(也是我的头脑中的):

l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
l2 = []
s = set()
for i in l1:
   if not i in s:
       l2.append(i)
       s.add(i)

This is off the top of my head (using dicts):

l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
l2 = []
s = {}
for i in l1:
    if not i in s:
        l2.append(i)
        s[i] = None

# l2 contains ['a', 2, 3, 0, 9.0, 6, 'b', 'a']

Edit: Using sets (also off the top of my head):

l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
l2 = []
s = set()
for i in l1:
   if not i in s:
       l2.append(i)
       s.add(i)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文