首先我们来看一下字符串使用默认的排序
list = ['Apple', 'Grape', 'Orange', 'Pear', 'Cheery', 'Bluebrrey', 'Dew']
print('排序前...’, ‘Orange’, ‘Pear’, ‘Cheery’, ‘Bluebrrey’, ‘Dew’]
按照长度升序排序:
[‘Dew’, ‘Pear’, ‘Apple’, ‘Grape’, ‘Orange...’, ‘Cheery’, ‘Bluebrrey’]
按照长度逆序排序:
[‘Bluebrrey’, ‘Orange’, ‘Cheery’, ‘Apple’, ‘Grape’, ‘Pear’, ‘Dew...现在我们发现其实编写的strlen函数就是一个一条语句的函数,我们可以使用lambda表达式代替这个函数
用lambda函数代替strlen函数
list = ['Apple', 'Grape', '...’, ‘Cheery’, ‘Bluebrrey’]
按照长度逆序排序:
[‘Bluebrrey’, ‘Orange’, ‘Cheery’, ‘Apple’, ‘Grape’, ‘Pear’, ‘Dew