Python3 sorted() 函数

2021/7/26 22:35:39

本文主要是介绍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标志,来设置升序或者降序。

 

 

 



 



这篇关于Python3 sorted() 函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程