#reversed()反转排序,可对列表、元组、区间等进行排序
#练习1
a = range(10)
a_list = [x for x in reversed(a)]
print(a_list)
#运行结果
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
#练习2
a = [1,2,3,4,5,6,7,8,9,10]
a_list = [x for x in reversed(a)]
print(a_list)
#运行结果
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
#练习3
str = 'English language'
str_list = [x for x in reversed(str)]
print(str_list)
#运行结果
['e', 'g', 'a', 'u', 'g', 'n', 'a', 'l', ' ', 'h', 's', 'i', 'l', 'g', 'n', 'E']
# sorted() 可以对所有可迭代的对象进行排序操作
#练习1
a = [20,12,0.5,63,-2]
print(sorted(a))
#运行结果
[-2, 0.5, 12, 20, 63]
#练习2
#根据字符串长度排序(key参数传入len函数)
b = ['fkit','crazyit','Charlie','fox','Emily']
print(sorted(b, key=len))
#运行结果
['fox', 'fkit', 'Emily', 'crazyit', 'Charlie']
#练习3
#通过sorted() 遍历
c = ['fkit','crazyit','Charlie','fox','Emily']
for i in sorted(c,key=len):
print(i)
#运行结果
fox
fkit
Emily
crazyit
Charlie