按字母顺序排序列表之前,请先用空格的字符串到一个列表中我有在单词之间的空格的字符串,例如:如何在Python
鱼狗屋苹果
我希望把它变成一个列表
[ '鱼', '狗', '房子', '苹果']
最后梳理该名单按字母顺序
[ '苹果', '狗', '鱼', '房子']
按字母顺序排序列表之前,请先用空格的字符串到一个列表中我有在单词之间的空格的字符串,例如:如何在Python
鱼狗屋苹果
我希望把它变成一个列表
[ '鱼', '狗', '房子', '苹果']
最后梳理该名单按字母顺序
[ '苹果', '狗', '鱼', '房子']
使用sorted()
排序
>>> data = 'fish dog house apple'
>>> sorted(data.split())
>>> ['apple', 'dog', 'fish', 'house']
,你可以这样做:
>>> l="fish dog house apple"
>>> l1=l.split(' ')
['fish', 'dog', 'house', 'apple']
>>> l1.sort()
>>> l1
['apple', 'dog', 'fish', 'house']
试试这个,
>>> a = "fish dog house apple"
>>> sorted(a.split())
['apple', 'dog', 'fish', 'house']
if you want reverse sort,
>>> sorted(a.split(), reverse=True)
['house', 'fish', 'dog', 'apple']
你已经尝试过什么来解决这个问题? – primoz