![]() |
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
|