对列表中的字符串按字母顺序排序

问题描述 投票:0回答:2

我有以下清单:

strs = ["tea","tea","tan","ate","nat","bat"]

我想对该列表中的字符串进行排序:

strs = ["aet","aet","ant","aet","ant","abt"]

我怎样才能做到这一点?

谢谢

python arrays list sorting
2个回答
4
投票

尝试如下:

>>> [''.join(sorted(s)) for s in strs]
['aet', 'aet', 'ant', 'aet', 'ant', 'abt']

# Or

>>> list(map(''.join, map(sorted, strs)))
['aet', 'aet', 'ant', 'aet', 'ant', 'abt']

1
投票
out = []
for val in strs:
    out.append("".join(sorted(list(val))))
print(out)
© www.soinside.com 2019 - 2024. All rights reserved.