a = 'abcxd'
print(reversed(a)) # <reversed object at 0x000000000356F048>
print(list(reversed(a))) # ['d', 'x', 'c', 'b', 'a']
print(sorted(a)) # ['a', 'b', 'c', 'd', 'x']
print(sorted(a, reverse=True)) # ['x', 'd', 'c', 'b', 'a']
b = [1, 8, 3, 6]
print(reversed(b)) # <list_reverseiterator object at ...
print(list(reversed(b))) # [6, 3, 8, 1]
print(sorted(b)) # [1, 3, 6, 8]
print(sorted(b, reverse=True)) # [8, 6, 3, 1]
b1 = [67, 'a', 89, 'b'] # 混合类型元素,无法排序
print(list(reversed(b1))) # ['b', 89, 'a', 67]
print(sorted(b1)) # TypeError: '<' not supported between instances of 'str' and 'int'
c = {'abcd': 101, 'xyz': 6, 'c': 33, 'az': 2}
# print(reversed(c)) # TypeError: 'dict' object is not reversible
print(sorted(c)) # ['abcd', 'az', 'c', 'xyz'] 默认按照 键 排序。如果键中有不同类型混合,排序出错
print(sorted(c, reverse=True)) # ['xyz', 'c', 'az', 'abcd']
print(sorted(c, key=lambda x: len(x))) # ['c', 'az', 'xyz', 'abcd'] 按照 键 长度 排序
print(sorted(c, key=lambda x: len(x), reverse=True)) # ['abcd', 'xyz', 'az', 'c']
new_c = {x: y for y, x in c.items() }
print(new_c) # {101: 'abcd', 6: 'xyz', 33: 'c', 2: 'az'}
print(max(new_c))
print(sorted(new_c)) # [2, 6, 33, 101]