Python序列可以通过内置函数sorted()对元素进行排序,也可以通过内置函数reversed()对元素进行反转。
示例代码如下:
# 对列表进行排序my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]sorted_list = sorted(my_list)print(sorted_list)# 对元组进行排序my_tuple = (3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)sorted_tuple = tuple(sorted(my_tuple))print(sorted_tuple)# 对字符串进行排序my_string = "hello world"sorted_string = ''.join(sorted(my_string))print(sorted_string)# 对列表进行反转reversed_list = list(reversed(my_list))print(reversed_list)# 对元组进行反转reversed_tuple = tuple(reversed(my_tuple))print(reversed_tuple)# 对字符串进行反转reversed_string = ''.join(reversed(my_string))print(reversed_string)以上代码分别对列表、元组和字符串进行了排序和反转操作。


