在另一个列表中排序列表

时间:2018-11-12 21:29:13

标签: python

我有一个像这样的列表b=['4 2 1 3 5']。 我想按数字排序,但无法这样做。我希望我的最终结果是b=['1 2 3 4 5']

2 个答案:

答案 0 :(得分:1)

您正在尝试对列表内的 string 进行排序,而不是对列表内的列表进行排序。哦,这是一种可能:

&sr=1500000010000&type=game&scoreA=x&scoreB=y&
&sr=1500000020000&type=game&scoreB=x&scoreB=y&
&sr=1500000030000&type=game&scoreC=x&scoreB=y&
&sr=1500000040000&type=game&scoreD=x&scoreB=y&

现在b = ['4 2 1 3 5'] b = [' '.join(sorted(b[0].split(), key=int))] 等于:

b

答案 1 :(得分:0)

当列表中有多个子列表时,以下代码将起作用。此外,如果不只是一个摘要数字(例如['4 41 42 44 5']),它也将起作用:

b = ['4 2 1 3 5', '8 9 1 2 3']
b = [" ".join([str(x) for x in sorted([int(x) for x in subLst.split(" ")])]) for subLst in b]
print(b)
相关问题