sort()方法:(用于列表)
sort(self, /, *, key=None, reverse=False)
sorted()方法:(任意可迭代对象)
sorted(iterable, /, *, key=None, reverse=False)
# 1.排序的元素不能两种不同类型的元素进行排序
>>>list1=[2,1,(3,4),6]
>>> list1.sort()
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
list1.sort()
TypeError: '<' not supported between instances of 'tuple' and 'int'
>>> list2=[6,1,'python',3]
>>> list2.sort()
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
list2.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
由上述两个例子可得出,只有两个数据类型相同的数据才能进行比较。
对list中的字符串进行排序 如果他内部同时含有英文字母和中文的话,优先对英文字母进行排序,再按照中文的ascl码排序,代码如下:
>>>