python on str.sort () method and sorted () function

list.sort method will place an ordered list, not a copy of the original list, which is the reason this method returns a value of None, this method does not remind New List

The built-in function sorted () will create a new one as the return value from this method can take any form of iteration object as a parameter, or even immutable sequence generator

Whether list.sort or sorted () has two optional keyword parameters 'reverse', it defaults to False, in ascending order, if set to True, was descending order.

When sorting a number of strings, may be used to achieve the sort key = str.lower case-insensitive, or use sort key = len string length based on

fruits = ['grape', 'raspberry', 'apple', 'banana']
print(fruits)
print(sorted(fruits))
print(sorted(fruits, reverse=True))
print(sorted(fruits, key=len))
print(sorted(fruits, key=len, reverse=True))
fruits.sort()
print(fruits)

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/Linux_liuge/article/details/94958486