how can I maintain sequence of my list using set?

徘徊边缘 提交于 2021-02-07 08:16:45

问题


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....


回答1:


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']



回答2:


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']



回答3:


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)


来源:https://stackoverflow.com/questions/3562971/how-can-i-maintain-sequence-of-my-list-using-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!