We have numbers in a string like this:
numbers = "1534423543"
We want to sort this and return:
"1,2,3,4,5"
(only unique numbers!)
How to do it in ONE line?
use set()
to get unique items, then sort them using sorted()
and finally join them using ",".join()
In [109]: strs="1534423543"
In [110]: ",".join(sorted(set(strs)))
Out[110]: '1,2,3,4,5'
Ashwini has the answer that's on the tip of everyone's fingers - if you're up for an import, you could do...
from itertools import groupby; ','.join(k for k, g in groupby(sorted(nums)))
And that's almost one line :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With