我在list
中有一个python
,如下所示,
l = [['4K', ['read', '10'], ['rand', '70']],
['64K', ['read', '15'], ['rand', '0']],
['64K', ['read', '0'], ['rand', '0']],
['4K', ['read', '60'], ['rand', '100']],
['4K', ['read', '50'], ['rand', '0']]]
如果temp = ['4K', ['read', '10'], ['rand', '100']
我首先要根据temp[2][1]
进行排序,之后我想进一步对列表进行排序,而不修改先前关于temp[1][1]
的排序。我希望最终结果如下,
res = [['64K', ['read', '0'], ['rand', '0']],
['64K', ['read', '15'], ['rand', '0']],
['4K', ['read', '50'], ['rand', '0']],
['4K', ['read', '10'], ['rand', '70'],
['4K', ['read', '60'], ['rand', '100']
]
在上面的res
中,首先将rand
优先级排序,然后排序read
,其中rand
按排序的l.sort(key=lambda x: int(x[2][1]))
排序。我尝试了rand
之类的事情,它首先对l
进行排序,但是进一步排序相同的rand
并没有给出预期的输出,因为之前的排序不会被记住。是否有捷径可寻?或者唯一的方法是使用相同的05-15 18:24:00.965 W/System.err(14449): android.content.pm.PackageManager$NameNotFoundException: id.co.aviana.brp
05-15 18:24:01.325 W/System.err(14449): android.content.pm.PackageManager$NameNotFoundException: id.co.aviana.brp
05-15 18:24:01.955 W/System.err(14449): android.content.pm.PackageManager$NameNotFoundException: id.co.aviana.brp
05-15 18:24:02.075 W/System.err(14449): android.content.pm.PackageManager$NameNotFoundException: id.co.aviana.brp
05-15 18:24:02.245 W/System.err(14449): android.content.pm.PackageManager$NameNotFoundException: id.co.aviana.brp
05-15 18:24:02.385 D/Wear_Controller(4835): Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=id.co.aviana.brpreload
05-15 18:24:03.235 W/BroadcastQueueInjector(1303): Unable to launch app com.syntellia.fleksy.keyboard/10125 for broadcast Intent { act=android.intent.action.PACKAGE_ADDED dat=package:id.co.aviana.brpreload flg=0x4000010 (has extras) }: process is not permitted to autostart
05-15 18:24:03.395 I/Finsky (14753): [1] com.google.android.finsky.utils.an.run(11): Package state data is missing for id.co.aviana.brpreload
05-15 18:24:14.745 I/ActivityManager(1303): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=id.co.aviana.brpreload cmp=id.co.aviana.brpreload/.SplashScreen} from uid 10082 on display 0
进一步拆分列表,然后在末尾对所有列表进行排序和合并?
答案 0 :(得分:3)
l.sort(key=lambda x: (int(x[2][1]), int(x[1][1])))
完全符合您的要求。元组按顺序排序优先。
答案 1 :(得分:1)
@ Oersted的回答是要走的路,但按多个标准排序时的一般规则是使用stable sort(which pythons list.sort
is)排序并向后排序标准,可以使用此规则从excel中的排序到基于Web的表的排序,在这种情况下,您将使用:
l.sort(key=lambda x: int(x[1][1]))
l.sort(key=lambda x: int(x[2][1]))