Python3 sorted() 函数

定义:

def sorted(*args, **kwargs): # real signature unknown
    """
    Return a new list containing all items from the iterable in ascending order.
    
    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.
    """
    pass

给它一个可迭代对象,返回一个按照升序的新的列表

用户可以自定义关键函数来实现排序,也可以设置反向标志设置为降序。

 

用法:

sorted(iterable, key=None, reverse=False)

 

key可以为自定义函数,函数只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。

reverse 默认False,代表升序,设置为True的时候,代表降序。


代码举例一:
import collections
from random import choice

Card = collections.namedtuple('Card', ['rank', 'suit'])


class FrenchDeck:
    ranks = [str(n) for n in range(3, 11)] + list("JQKA2")
    suits = 'spades dimonds clubs hearts'.split(" ")

    def __init__(self):
        self._cards = [Card(rank, suit) for rank in self.ranks
                       for suit in self.suits]

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]


deck = FrenchDeck()
suit_values = dict(spades=3, dimonds=2, clubs=1, hearts=0)


def spades_high(card):
    rank_value = FrenchDeck.ranks.index(card.rank)
    return rank_value * 4 + suit_values[card.suit]


for card in sorted(deck, key=spades_high):
    print(card)

代码中,deck是个可迭代对象,deck打印出如下

Card(rank='3', suit='spades')
Card(rank='3', suit='dimonds')
Card(rank='3', suit='clubs')
Card(rank='3', suit='hearts')
Card(rank='4', suit='spades')
Card(rank='4', suit='dimonds')
Card(rank='4', suit='clubs')
Card(rank='4', suit='hearts')
Card(rank='5', suit='spades')
Card(rank='5', suit='dimonds')
Card(rank='5', suit='clubs')
Card(rank='5', suit='hearts')
Card(rank='6', suit='spades')
Card(rank='6', suit='dimonds')
Card(rank='6', suit='clubs')
Card(rank='6', suit='hearts')
Card(rank='7', suit='spades')
Card(rank='7', suit='dimonds')
Card(rank='7', suit='clubs')
Card(rank='7', suit='hearts')
Card(rank='8', suit='spades')
Card(rank='8', suit='dimonds')
Card(rank='8', suit='clubs')
Card(rank='8', suit='hearts')
Card(rank='9', suit='spades')
Card(rank='9', suit='dimonds')
Card(rank='9', suit='clubs')
Card(rank='9', suit='hearts')
Card(rank='10', suit='spades')
Card(rank='10', suit='dimonds')
Card(rank='10', suit='clubs')
Card(rank='10', suit='hearts')
Card(rank='J', suit='spades')
Card(rank='J', suit='dimonds')
Card(rank='J', suit='clubs')
Card(rank='J', suit='hearts')
Card(rank='Q', suit='spades')
Card(rank='Q', suit='dimonds')
Card(rank='Q', suit='clubs')
Card(rank='Q', suit='hearts')
Card(rank='K', suit='spades')
Card(rank='K', suit='dimonds')
Card(rank='K', suit='clubs')
Card(rank='K', suit='hearts')
Card(rank='A', suit='spades')
Card(rank='A', suit='dimonds')
Card(rank='A', suit='clubs')
Card(rank='A', suit='hearts')
Card(rank='2', suit='spades')
Card(rank='2', suit='dimonds')
Card(rank='2', suit='clubs')
Card(rank='2', suit='hearts')

spades_high是自定义函数,此函数参数取自于可迭代对象deck中,此处为deck中52个由namedtuple创建的名叫Card的元祖对象,通过对spades_high函数的输出数值进行升序排序。

最后排序之后的输出如下:

Card(rank='3', suit='hearts')
Card(rank='3', suit='clubs')
Card(rank='3', suit='dimonds')
Card(rank='3', suit='spades')
Card(rank='4', suit='hearts')
Card(rank='4', suit='clubs')
Card(rank='4', suit='dimonds')
Card(rank='4', suit='spades')
Card(rank='5', suit='hearts')
Card(rank='5', suit='clubs')
Card(rank='5', suit='dimonds')
Card(rank='5', suit='spades')
Card(rank='6', suit='hearts')
Card(rank='6', suit='clubs')
Card(rank='6', suit='dimonds')
Card(rank='6', suit='spades')
Card(rank='7', suit='hearts')
Card(rank='7', suit='clubs')
Card(rank='7', suit='dimonds')
Card(rank='7', suit='spades')
Card(rank='8', suit='hearts')
Card(rank='8', suit='clubs')
Card(rank='8', suit='dimonds')
Card(rank='8', suit='spades')
Card(rank='9', suit='hearts')
Card(rank='9', suit='clubs')
Card(rank='9', suit='dimonds')
Card(rank='9', suit='spades')
Card(rank='10', suit='hearts')
Card(rank='10', suit='clubs')
Card(rank='10', suit='dimonds')
Card(rank='10', suit='spades')
Card(rank='J', suit='hearts')
Card(rank='J', suit='clubs')
Card(rank='J', suit='dimonds')
Card(rank='J', suit='spades')
Card(rank='Q', suit='hearts')
Card(rank='Q', suit='clubs')
Card(rank='Q', suit='dimonds')
Card(rank='Q', suit='spades')
Card(rank='K', suit='hearts')
Card(rank='K', suit='clubs')
Card(rank='K', suit='dimonds')
Card(rank='K', suit='spades')
Card(rank='A', suit='hearts')
Card(rank='A', suit='clubs')
Card(rank='A', suit='dimonds')
Card(rank='A', suit='spades')
Card(rank='2', suit='hearts')
Card(rank='2', suit='clubs')
Card(rank='2', suit='dimonds')
Card(rank='2', suit='spades')

 

代码举例二:

a = [-5, -3, -1, 0, 2, 4, 6]
print(sorted(a, key=lambda x: x**2))
print(sorted(a, key=lambda x: x**2, reverse=True))

[0, -1, 2, -3, 4, -5, 6]
[6, -5, 4, -3, 2, -1, 0]

即按照lambda函数:x的平方值的升序,来对列表a排序,通过设置reverse标志,来设置升序或者降序。

 

 

 




 

上一篇:mysql 排序 (row_number、dense_rank、rank)索引(主键索引、唯一索引、普通索引、全文索引、多列索引)语句执行顺序


下一篇:【高等代数】7. 矩阵(2)