string, split, sort, None, huh?

Sean 'Shaleh' Perry shaleh at valinux.com
Sat Feb 17 00:21:12 CET 2001


On 16-Feb-2001 Phlip wrote:
> Thy Pon:
> 
> Gonna split a document by linefeeds into strings, then sort the strings. 
> Child's play, huh?
> 
>         lines = split ( """Mary
> had
> a
> little
> lamb""", "\n" )
> 
>         print repr(lines)
>         print repr(lines.sort())
> 

list.sort() does not return a list, it sorts it in place.

>>> string = "Mary\nhad\na\nlittle\nlamb."
>>> print string
Mary
had
a
little
lamb.
>>> from string import split
>>> pieces = split(string, '\n')
>>> print pieces
['Mary', 'had', 'a', 'little', 'lamb.']
>>> pieces.sort()
>>> print pieces
['Mary', 'a', 'had', 'lamb.', 'little']





More information about the Python-list mailing list