按两个字段对 Python 列表进行排序 [英] Sorting a Python list by two fields

查看:35
本文介绍了按两个字段对 Python 列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从排序的 csv 创建了以下列表

I have the following list created from a sorted csv

list1 = sorted(csv1, key=operator.itemgetter(1))

我实际上想按两个条件对列表进行排序:首先按字段 1 中的值,然后按字段 2 中的值.我该怎么做?

I would actually like to sort the list by two criteria: first by the value in field 1 and then by the value in field 2. How do I do this?

推荐答案

像这样:

import operator
list1 = sorted(csv1, key=operator.itemgetter(1, 2))

这篇关于按两个字段对 Python 列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆