Python:list.sort()似乎不起作用

时间:2012-07-31 23:23:56

标签: python

>>> x = set(['216', '217', '214', '215', '212', '213', '210', '211', '165', '264', '218', '219', '133', '132', '131', '130', '137', '136', '135', '226', '139', '138', '166', '24', '25', '26', '27', '20', '21', '22', '23', '95', '28', '29', '222', '288', '346', '4', '161', '8', '163', '348', '119', '868', '258', '120', '121', '122', '260', '124', '125', '126', '127', '128', '129', '167', '118', '59', '58', '55', '54', '57', '56', '51', '50', '53', '52', '298', '292', '293', '164', '201', '199', '179', '200', '195', '194', '706', '134', '191', '190', '193', '192', '115', '114', '88', '89', '111', '110', '113', '176', '82', '83', '80', '81', '86', '87', '84', '85', '117', '198', '3', '177', '171', '7', '247', '309', '225', '306', '307', '178', '244', '108', '109', '241', '240', '102', '103', '100', '101', '106', '107', '104', '105', '39', '38', '33', '32', '31', '30', '37', '36', '35', '34', '642', '181', '393', '205', '223', '207', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '175', '174', '173', '172', '254', '170', '203', '249', '248', '182', '183', '180', '2', '162', '187', '184', '6', '220', '188', '189', '197', '202', '313', '196', '221', '185', '99', '98', '168', '169', '229', '228', '91', '90', '93', '92', '160', '94', '97', '96', '11', '10', '13', '12', '15', '14', '17', '16', '19', '18', '272', '250', '116', '204', '151', '150', '153', '152', '155', '154', '157', '156', '159', '158', '112', '279', '234', '235', '236', '237', '231', '232', '224', '48', '49', '46', '47', '44', '45', '42', '43', '40', '41', '1', '320', '5', '9', '146', '147', '144', '145', '142', '143', '140', '141', '208', '394', '148', '149', '77', '76', '75', '74', '73', '72', '71', '70', '79', '78', '263', '262', '472', '123', '276'])
>>> type(x)

    <type 'set'>

现在我创建一个列表

>>> y = list(x)
>>> type(y)
<type 'list'>

现在我排序

>>> y.sort()

>>> y

['1', '10', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '11', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '12', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '13', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '14', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '15', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '16', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '17', '170', '171', '172', '173', '174', '175', '176', '177', '178', '179', '18', '180', '181', '182', '183', '184', '185', '187', '188', '189', '19', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '2', '20', '200', '201', '202', '203', '204', '205', '207', '208', '21', '210', '211', '212', '213', '214', '215', '216', '217', '218', '219', '22', '220', '221', '222', '223', '224', '225', '226', '228', '229', '23', '231', '232', '234', '235', '236', '237', '24', '240', '241', '244', '247', '248', '249', '25', '250', '254', '258', '26', '260', '262', '263', '264', '27', '272', '276', '279', '28', '288', '29', '292', '293', '298', '3', '30', '306', '307', '309', '31', '313', '32', '320', '33', '34', '346', '348', '35', '36', '37', '38', '39', '393', '394', '4', '40', '41', '42', '43', '44', '45', '46', '47', '472', '48', '49', '5', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '6', '60', '61', '62', '63', '64', '642', '65', '66', '67', '68', '69', '7', '70', '706', '71', '72', '73', '74', '75', '76', '77', '78', '79', '8', '80', '81', '82', '83', '84', '85', '86', '868', '87', '88', '89', '9', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']

但是检查y中的元素,它们似乎没有排序。这有什么不对吗?

2 个答案:

答案 0 :(得分:14)

如果您想将它们排序为int s,即数字,而不是按字典顺序排序,请使用y = sorted(x, key=int)

答案 1 :(得分:3)

因为这些是你要排序的字符串,例如'1','2'和'100',所以它们按构成字符串的字节排序..而不是字符串表示的数字。

因为表示字符“1”的字节小于表示字符“2”的字节,所以以“1”开头的所有字符串都将是第一个。等等。

您需要将字符串转换为整数,然后对它们进行排序以使它们按数字顺序排序。