简体   繁体   English

在 Python 中的列表中排序列表

[英]Sorting lists within a list in Python

I have a list J consisting of lists.我有一个由列表组成的列表J I am trying to sort elements of each list in ascending order.我正在尝试按升序对每个列表的元素进行排序。 I present the current and expected outputs.我介绍了当前和预期的输出。

J=[[10, 4, 7], [10, 4],[1,9,8]]
for i in range(0,len(J)):
    J[0].sort()

The current output is当前的 output 是

[[4, 7, 10], [10, 4], [1, 9, 8]]

The expected output is预期的 output 是

[[4, 7, 10], [4, 10], [1, 8, 9]]

Just remove the range只需删除范围

J=[[10, 4, 7], [10, 4],[1,9,8]]
for i in J:
    i.sort()
print(J)

Output: Output:

[[4, 7, 10], [4, 10], [1, 8, 9]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM