/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
def g(l, n): return zip(*[iter(l)]*n) ''' >>> g(range(10), 3) [(0, 1, 2), (3, 4, 5), (6, 7, 8)] >>> g('hello world!', 2) [('h', 'e'), ('l', 'l'), ('o', ' '), ('w', 'o'), ('r', 'l'), ('d', '!')] Yeah, course. g groups a list l into a list of n-tuples, by taking each group of n elements from the list and making them into a tuple. How useful. '''
URL: http://drj11.wordpress.com/2009/01/28/my-python-dream-about-groups/#