how to flatten & lump lists
cpsoct at my-deja.com
cpsoct at my-deja.com
Sun Dec 31 09:01:19 EST 2000
I want to be able to flatten a list and i could have sworn that i saw
that there was something like flatten() built in someplace but i can't
find it now anywhere in the docs.
if i have the following code:
# mirror.py -- [1,2,3] returns [1,2,3,2,1]
# how to make it return [1,2,3,2,1,2,3,2,1]
# this should really be a tuple so that it is preserved.
l=[1, 2, 3, 4, 5, 6, 7]
foo=l[:-1] #copy list, excluding last element
foo.reverse() #flip it
l.append(foo) #stick it on the end of the other list
#we need to flatten it some how in to a single list
print l
the flipped part (foo) tacked on to the end of the list as a single
element instead of a single long list.
[1, 2, 3, 4, 5, 6, 7, [6, 5, 4, 3, 2, 1]]
how can i get: [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]?
additionally i would like to do the opposite, take a list like
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and have it lump up into subgroups (randomly) and return:
[[1],[2],[3, 4, 5], [6, 7], [8, 9, 10]]
Any help would be appreciated. I have lots of list processing ideas that
i want to impliment, but i need to know this first. Additionally, if
anyone knows of such list processing and pattern making funcs, please
let me know. i found a few on "vaults" but not of the type i was hoping
for.
happy new year,
kevin parks
seoul, korea
cpsoct at lycos.com
Sent via Deja.com
http://www.deja.com/
More information about the Python-list
mailing list