列表排序

作者: 右哼哼丨左哼哼 | 来源:发表于2018-01-29 11:55 被阅读2次

要求将如下列表的(x,y)变为(y,x),例:list1=[('me',20),....]变为list1=[(20,'me'),....]

list1=[('me',20),('he',15),('her',23)]
list2=[(15,'he'),(20,'me'),(23,'her')]

解法

>>> list1=[('me',20),('he',15),('her',23)]
>>> items=[[x,y] for (y,x) in list1]
>>> items
[[20, 'me'], [15, 'he'], [23, 'her']]
>>> items.sort()
>>> items
[[15, 'he'], [20, 'me'], [23, 'her']]

相关文章

网友评论

    本文标题:列表排序

    本文链接:https://www.haomeiwen.com/subject/qasuaxtx.html