split a string and build tuple
Jonathan Daugherty
cygnus at cprogrammer.org
Thu Feb 5 15:58:28 EST 2004
# 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)
>>> import string
>>> s = "w1 w2 w3 w4 w5 w6 w7 w8"
>>> x = string.split(s)
>>> x
['w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7', 'w8']
>>> for index in range(0, len(x), 3):
... print x[index:index + 3]
...
['w1', 'w2', 'w3']
['w4', 'w5', 'w6']
['w7', 'w8']
--
Jonathan Daugherty
http://www.cprogrammer.org
"It's a book about a Spanish guy called Manual, you should read it."
-- Dilbert
More information about the Python-list
mailing list