Python 中的字符串没有可以直接排序的方法,需要转成list(char),用 sorted
排序后再合并。
使用
join
合并最简单,推荐。
s2 = ''.join(sorted('s1'))
使用
reduce
高阶函数合并,复杂,不推荐。
res = reduce(lambda x, y: x + y, sorted(test_string))
Python 中的字符串没有可以直接排序的方法,需要转成list(char),用 sorted
排序后再合并。
使用
join
合并最简单,推荐。
s2 = ''.join(sorted('s1'))
使用
reduce
高阶函数合并,复杂,不推荐。
res = reduce(lambda x, y: x + y, sorted(test_string))