Dav*_*lva 9 python sorting string
我们在这样的字符串中有数字:
numbers = "1534423543"
Run Code Online (Sandbox Code Playgroud)
我们想对此进行排序并返回:
"1,2,3,4,5"
Run Code Online (Sandbox Code Playgroud)
(只有唯一的数字!)
如何在一行中完成?
Ash*_*ary 28
使用set()
获得独特的物品,然后用它们排序sorted()
,最后用加入他们的行列",".join()
In [109]: strs="1534423543"
In [110]: ",".join(sorted(set(strs)))
Out[110]: '1,2,3,4,5'
Run Code Online (Sandbox Code Playgroud)
Ashwini的答案就在每个人的手指上 - 如果你想要进口,你可以......
from itertools import groupby; ','.join(k for k, g in groupby(sorted(nums)))
Run Code Online (Sandbox Code Playgroud)
这几乎是一行:)