简体   繁体   中英

Python list generate ascending order

  List1 = ['ab_01:2,20,100', 'ab_02:1,300,10', "ab_03:5,400,22","ab_04:8,5050,22"]

I have list how to make it in ascending order. Only check after colon value (in this case 2,1,5,8) to decide ordering and keep all values as it is.

Expected Output:

   List1 = ['ab_02:1,300,10', 'ab_01:2,20,100', "ab_03:5,400,22","ab_04:8,5050,22"]

If it is only numeric then I could have to use sorted(list1, key=int).

The reason why i want this is, I want to iterate from lower value in for loop.

Code I tried and struck in middle to proceed..

List2 = []
for x in List1:
    a = x.split(":")[0].split(",")[0]
    List2.append(a)
sorted(List2, key=int)

尝试这样的事情:

sorted(List1, key=lambda x: x.split(':')[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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