how can I maintain sequence of my list using set?

2024/9/20 7:51:11
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....

Answer

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']
https://en.xdnf.cn/q/72520.html

Related Q&A

How to reference a dict object?

I have a Python dict object d. d = {a: 1, b: 2, c: 3}. My problem is really simple. I want to reference a variable to the elements of d. For example, something like:In[1]: p = d[a] >>> p = 1 I…

Python 3.3: DeprecationWarning when using nose.tools.assert_equals

I am using nosetest tools for asserting a python unittest:... from nose.tools import assert_equals, assert_almost_equalclass TestPolycircles(unittest.TestCase):def setUp(self):self.latitude = 32.074322…

Panel/Hvplot interaction when variable is changing

Im trying to create a dashboard with two holoviews objects: a panel pn.widgets.Select object that contains a list of xarray variables, and a hvplot object that takes the selected variable on input, lik…

asyncio matplotlib show() still freezes program

I wish to run a simulation while at the same time output its progress in a plot. Ive been looking through a lot of examples of threading and multiprocessing, but they are all pretty complex. So I thoug…

celery .delay hangs (recent, not an auth problem)

I am running Celery 2.2.4/djCelery 2.2.4, using RabbitMQ 2.1.1 as a backend. I recently brought online two new celery servers -- I had been running 2 workers across two machines with a total of ~18 thr…

Python ttk.combobox force post/open

I am trying to extend the ttk combobox class to allow autosuggestion. the code I have far works well, but I would like to get it to show the dropdown once some text has been entered without removing fo…

How to pull from the remote using dulwich?

How to do something like git pull in python dulwich library.

convert python programme to windows executable

i m trying to create windows executable from python program which has GUI . i m using following scriptfrom distutils.core import setup import py2exesetup(console=[gui.py]) it gives following errorWarni…

Must explicitly set engine if not passing in buffer or path for io in Panda

When running the following Python Panda code:xl = pd.ExcelFile(dataFileUrl)sheets = xl.sheet_namesdata = xl.parse(sheets[0])colheaders = list(data)I receive the ValueError:Must ex…

How to access the keys or values of Python GDB Value

I have a struct in GDB and want to run a script which examines this struct. In Python GDB you can easily access the struct via(gdb) python mystruct = gdb.parse_and_eval("mystruct")Now I got t…