Python 字符串排序

133 阅读1分钟

Python 中的字符串没有可以直接排序的方法,需要转成list(char),用 sorted 排序后再合并。

使用join合并最简单,推荐。

s2 = ''.join(sorted('hello world.'))

使用 reduce 高阶函数合并,较复杂,不推荐。

res = reduce(lambda x, y: x + y, sorted('hello world.'))