简体   繁体   English

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

[英]Python: list.sort() doesn't seem to work

>>> 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'>

Now I create a list 现在我创建一个列表

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

Now I sort 现在我整理

>>> 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']

But check elements in y, they doesn't seem to be sorted. 但是检查y中的元素,它们似乎没有排序。 Whats wrong here? 怎么了

如果要按int的形式对它们进行排序,即按数字而不是按字典顺序y = sorted(x, key=int) ,请使用y = sorted(x, key=int)

Because these are strings you are sorting eg '1','2' and '100', they are ordered by the bytes that make up the string.. not the number the string represents. 因为这些是您要排序的字符串,例如“ 1”,“ 2”和“ 100”,所以它们是按构成字符串的字节排序的。而不是字符串表示的数字。

Because the bytes that represent the character '1' is less that the byte that represents the character '2', all the strings starting with '1' will be first. 因为代表字符“ 1”的字节少于代表字符“ 2”的字节,所以所有以“ 1”开头的字符串都将是第一个。 etc etc. 等等等

You need do convert the strings to integers and then sort them to make them sort in numerical order. 您需要将字符串转换为整数,然后对它们进行排序以使其按数字顺序排序。

G./ G。/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM