1
0
你可以用
输出:
|
2
1
这可能比你要求的要复杂一点。我假设:
因此,您可以使用以下代码:
records = [[['Jack', 'male', 1],['Jack', 'male', 2],['Jack', 'male', 3]],[['Sally', 'female', 1],['Sally', 'female', 2],['Sally', 'female', 3]]]
list_order = ['female', 'male']
# "flatten" list into singly-nested list
records = [leaf for branch in records for leaf in branch]
# sort the items in the list by list_order
# sorted passes each item in the list to the lambda function
# record[1] is the position of "male"/"female"
records = sorted(records, key=lambda record: list_order.index(record[1]))
# group the list by list_order again, creating doubly-nested list
records = [ [record for record in records if record[1] == item ] for item in list_order ]
print records
|
July · 如何定义数字间隔,然后四舍五入 1 年前 |
user026 · 如何根据特定窗口的平均值(行数)创建新列? 1 年前 |
Ashok Shrestha · 需要追踪特定的颜色线并获取坐标 1 年前 |
Nicote Ool · 在FastApi和Vue3中获得422 1 年前 |
Abdulaziz · 如何对集合内的列表进行排序[重复] 1 年前 |
asmgx · 为什么合并数据帧不能按照python中的预期方式工作 1 年前 |