6761

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

Question:

>>> 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. Whats wrong here?

Answer1:

If you want to sort them as ints, i.e. numerically, not lexicographically, use y = sorted(x, key=int).

Answer2:

Because these are strings you are sorting e.g '1','2' and '100', they are ordered by the bytes that make up the string.. not the number the string represents.

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. etc etc.

You need do convert the strings to integers and then sort them to make them sort in numerical order.

G./

Recommend

  • What is the comparison algorithm used in “strcmp” - C programming?
  • How to sort “chapter numbers” like 1.1.1, 1.2.1, 1.2.46, etc.?
  • Highlight mean value on y-axis in bar chart
  • (Python) Script to determine if (x, y) coordinates are colinear - getting some errors
  • Morgan and a String Algorithm
  • Converting a list of strings to ints (or doubles) in Python
  • How to copy pandas DataFrame values down to fill 0's?
  • Inserting Array References into Perl Heap
  • Division in C# to get exact value [duplicate]
  • python object not iterable error in function
  • Is creating a new version of an object in AWS S3 eventually consistent or read-after-write consisten
  • Python find continuous interesctions of intervals
  • Is there a difference between initializing a variable in the default constructor or inline? [duplica
  • integration of function in modelica
  • Call a php script whenever an e-mail is received?
  • Many to Many in Linq using Dapper
  • How to turn (A, B, C) into (AB, AC, BC) with Pig?
  • Sort List of Strings By Version
  • How to suppress a dialog
  • Atlas images wrong size on iPad iOS 9
  • Meteor: Do Something On Email Verification Confirmation
  • Why value captured by reference in lambda is broken? [duplicate]
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Where to put my custom functions in Wordpress?
  • When should I choose bucket sort over other sorting algorithms?
  • RestKit - RKRequestDelegate does not exist
  • Unanticipated behavior
  • WPF Applying a trigger on binding failure
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Why joiner is not used after Sequence generator or Update statergy
  • How do I configure my settings file to work with unit tests?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Recursive/Hierarchical Query Using Postgres
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal