аватар question@mail.ru · 01.01.1970 03:00

Line conversion to sorting in lexicographic order

How to convert a string consisting of words separated by spaces, and sort the resulting list in a lexicographic order?

is given a line from words separated by spaces:

  S =  'ABC a BCD BC ABC BC BCD BCD ABC'      

We convert it to the "Space" "" Space "":

  A = S.Split ( '' )     

we get:

  [ 'Abc' ,  '' a ', ' bcd ', ' bc ',  'Abc' ,  'bc' ,  'bcd' ,  '' bcd ',  'ABC' ]     

class = ""> print (a)

we will not get anything if:

  b = a.sort ()   Print  (a)     

We get the sorted list

  [ 'ABC' ,  'Abc' ,  'bc' ,  '' "> '' bcd ', '` `` span>,  'ABC' ,  'bc' ,  'bcd' ,  '' "> ''"> '' "]   

why? Is it possible to somehow combine sort () and split () in one line?

аватар answer@mail.ru · 01.01.1970 03:00

This is simple:

  a =  sorted  (s.split ( '' ' '))  

in the reverse order:

  a =  sorted  (s.split ( '' ), reverse =  true )                                       

Latest

Similar