Unique lists from a list

2024/10/14 19:28:16

Given a list I need to return a list of lists of unique items. I'm looking to see if there is a more Pythonic way than what I came up with:

def unique_lists(l):m = {}for x in l:m[x] = (m[x] if m.get(x) != None else []) + [x]return [x for x in m.values()]    print(unique_lists([1,2,2,3,4,5,5,5,6,7,8,8,9]))

Output:

[[1], [2, 2], [3], [4], [5, 5, 5], [6], [7], [8, 8], [9]]
Answer
>>> L=[1,2,2,3,4,5,5,5,6,7,8,8,9]
>>> from collections import Counter
>>> [[k]*v for k,v in Counter(L).items()]
[[1], [2, 2], [3], [4], [5, 5, 5], [6], [7], [8, 8], [9]]
https://en.xdnf.cn/q/69378.html

Related Q&A

How to run Spyder with Python 3.7 with Anaconda

I have installed Anaconda on a Windows 10 machine which comes with Spyder and Python 3.6 but I wish to upgrade to Python 3.7To create an Anaconda Environment with Python 3.7 is easy by using:conda crea…

Pass in a list of possible routes to Flask?

Im learning Flask and have a question about dynamic routing: is it possible to pass in a list of accepted routes? I noticed the any converter which has potential but had a hard time finding examples o…

Tornado @run_on_executor is blocking

I would like to ask how tornado.concurrent.run_on_executor (later just run_on_executor) works, because I probably do not understand how to run synchronous task to not block the main IOLoop.All the exa…

Using Pandas read_csv() on an open file twice

As I was experimenting with pandas, I noticed some odd behavior of pandas.read_csv and was wondering if someone with more experience could explain what might be causing it.To start, here is my basic cl…

Disable Jedi linting for Python in Visual Studio Code

I have set my linter for Python to Pylint, but I still get error messages from Jedi. I even went to settings.json and added the line "python.linting.jediEnabled": false, but the line, though …

Bar plot with timedelta as bar width

I have a pandas dataframe with a column containing timestamps (start) and another column containing timedeltas (duration) to indicate duration.Im trying to plot a bar chart showing these durations with…

Take screenshot of second monitor with python on OSX

I am trying to make an ambient light system with Python. I have gotten pyscreenshot to save a screenshot correctly, but I cant figure out how to get it to screenshot my second monitor (if this is even …

Invoking the __call__ method of a superclass

http://code.google.com/p/python-hidden-markov/source/browse/trunk/Markov.pyContains a class HMM, which inherits from BayesianModel, which is a new-style class. Each has a __call__ method. HMMs __call__…

Efficent way to split a large text file in python [duplicate]

This question already has answers here:Sorting text file by using Python(3 answers)Closed 10 years ago.this is a previous question where to improve the time performance of a function in python i need t…

Creating a unique id in a python dataclass

I need a unique (unsigned int) id for my python data class. This is very similar to this so post, but without explicit ctors. import attr from attrs import field from itertools import count @attr.s(aut…