mixed

Sorting a mixed list of ints and strings

♀尐吖头ヾ 提交于 2019-11-26 18:41:14
问题 I am trying to sort the following mixed list of ints and strings, but getting a TypeError instead. My desired output order is sorted integers then sorted strings. x=[4,6,9,'ashley','drooks','chay','poo','may'] >>> x.sort() Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> x.sort() TypeError: '<' not supported between instances of 'str' and 'int' 回答1: You can pass a custom key function to list.sort: x = [4,6,9,'ashley','drooks','chay','poo','may'] x.sort(key=lambda v: