split a string and build tuple

Lou Pecora pecora at anvil.nrl.navy.mil
Thu Feb 5 16:28:34 EST 2004


In article <4022acc0$0$2959$626a54ce at news.free.fr>,
 "GrelEns" <grelens at NOSPAMyahoo.NOTNEEDEDfr> wrote:

> hello,
> 
> i'm looking for a pythonic and obvious way to do the following :
> 
> having a string space separated like : s = "w1 w2 w3 w4 w5 w6 w7 w8..."
> how to get : l = (('w1', 'w2', 'w3'), ('w4', 'w5', 'w6'), ('w7', 'w8', ...),
> ...) ?
> (a kind of splitting and grouping in an elegant way)
> 
> i think that list comprehension or "zip" could maybe help but i just can't
> get it work,
> 
> thanks.

Since it is space-separated use the Python function 'split' to get a 
list of the separate "words."  Then you can group them however you like 
using list functions.

-- 
-- Lou Pecora
     My views are my own.



More information about the Python-list mailing list