开发者

Format a nested list in place

开发者 https://www.devze.com 2023-03-16 05:24 出处:网络
I have the following list of tuples -- [(\'one\',[1,2,5,3,9,8]), (\'two\',[9,8,5,1])] And need to sort the nested list, while keeping the ordering of the tuples as is --

I have the following list of tuples --

[('one',[1,2,5,3,9,8]), ('two',[9,8,5,1])]

And need to sort the nested list, while keeping the ordering of the tuples as is --

[('one',[1,2,3,5,8,9]), ('two',[1,5,8,9])]

The way that I am currently doing this is with a for loop --

list_of_tuples = [('one',[1,2,5,3,9,8]), ('two',[9,8,5,1])]

sorted_list_of_tuples = []
for item1, item2 in list_of_tuples:
    sorted_list_of_tuples.append((item1, sorted开发者_C百科(item2))

Is there an easier way to do this in one line? Thank you.


Doesn't get much easier:

for t in list_of_tuples:
    t[1].sort()


Just us a list comprehension.

sorted_list = [(x[0],sorted(x[1])) for x in list_of_tuples]
0

精彩评论

暂无评论...
验证码 换一张
取 消