Max*_*888 -3 python python-3.x
我有一个有序列表:
ordering = ["red", "green", "blue", "yellow", "orange"]
Run Code Online (Sandbox Code Playgroud)
和一个较小的无序列表,我想根据这个列表进行排序:
unordered = ["blue", "orange", "red"]
Run Code Online (Sandbox Code Playgroud)
给出结果:
["red", "blue", "orange"]
Run Code Online (Sandbox Code Playgroud)
这似乎是已经被问到的那种问题,但我已经完成了所有我能找到的类似措辞的问题,但没有一个能解决这个问题。
sort()有一个参数可以让你编写一个排序函数。您可以按如下方式使用此排序功能:
unordered.sort(key=lambda x: ordering.index(x))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43 次 |
| 最近记录: |