mapping a statement over all list items

radix radix at twistedmatrix.com
Sun Nov 11 10:19:58 CET 2001


On Sun, 11 Nov 2001 04:10:15 -0500, Kevin Christie wrote:

> Hello all!
> 
>    I have list Foo, of strings. Each string has a special character '-'
> within the string. What I want is for Python to split each string list
> item by '-' and assign the resulting lists to the original list item.
> Basically, I want to apply:
> 
> Foo[x] = Foo[x].split('-')
> 
> over all items x in Foo, in one clean statement.
> 
> Thus if Foo was ['abc-xyz', 'black-blue', '123-987'], the result after
> mapping to Foo would be:
> 
> Foo = [ ['abc', 'xyz'], ['black','blue'], ['123','987'] ]
> 

Here you go:

>>> [string.split(x, '-') for x in l]
[['abc', 'xyz'], ['black', 'blue'], ['123', '987']]

of course, only in python2.0 or greater. 

-- 
<glyph> that's why I love IRC 
<glyph> you can't be late for IRC 
--
                              Chris Armstrong
                      <<< radix at twistedmatrix.com >>>
              http://twistedmatrix.com/users/carmstro.twistd/



More information about the Python-list mailing list