请问使用sorted()函数对列表进行临时排序中“如何按与字母顺序相反的顺序显示列表”

书里有这样一串代码
cars = [‘bmw’,’audi’,’toyota’,’subaru’]

print(“Here is the original list:”)
print(cars)

print(“\nHere is the sorted list:”)
print(sorted(cars))

print(“\nHere is the original list again:”)
print(cars)

下面有一串解释
注意,在调用sorted()函数后,列表元素的排列顺序并没有变。如果要按与字母顺序相反的顺序显示列表,也可向sorted()函数传递参数reserve=True
问:如何运用书中提示来运用reserve=True,麻烦各位大神帮忙解疑答惑一下,谢谢

讨论数量: 1
Jason990420
>>> help(sorted)
Help on built-in function sorted in module builtins:

sorted(iterable, /, *, key=None, reverse=False)
    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.
cars = ['bmw','audi','Toyota','subaru']
print(sorted(
    cars,
    # key=str,
    reverse=True),
)
['subaru', 'bmw', 'audi', 'Toyota']
print(sorted(
    cars,
    key=str.lower,
    reverse=True),
)
['Toyota', 'subaru', 'bmw', 'audi']
16小时前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!