List
Chris Gonnerman
chris.gonnerman at newcenturycomputers.net
Sun Jun 2 17:40:09 EDT 2002
----- Original Message -----
From: "Gold Fish" <occeanlinux at linuxmail.org>
> What i would achieved is that I got the list of elements
> say that 10 elements in the list, i want to seperate
> this list into 2,or 3 sublists depends on user input the
> number. Then these subject contain the number of
> elements say 2, or 3 depend on user define. How can i do
> this. For example.
>
> BigList = [e1,e2,e3,e4,e5,e6,e7]
>
> this will be divided into 4 small sublists which is
> sublist1,sublist2,sublist3,sublist4.These sublists will
> have maximum of 2 elements inside therefore, sublist1 =
> [e1,e2], sublist2=[e3,e4],sublist3=[e5,e6] and sublist7=
> [e7]
>
> I would appreciate that you could help me this problem.
> I spend so much time to think about it but it's look
> like i stuck in the maze when trying to divide biglist
> into small list and small list to nano list.
Given a list of unknown size, subdividing it into sublists
could go like this:
Lists = []
N = 2
for i in range(len(BigList)/N):
Lists.append(BigList[i*N:(i+1)*N])
so that given your BigList:
BigList = [e1,e2,e3,e4,e5,e6,e7]
the result is:
[['e1', 'e2'], ['e3', 'e4'], ['e5', 'e6']]
Note that e7 is missed; if the list you are subdividing
does not divide evenly by N, an adjustment must be made:
Lists = []
N = 2
adj = 0
if len(BigList)%N:
adj = 1
for i in range(len(BigList)/N+adj):
Lists.append(BigList[i*N:(i+1)*N])
(If anyone knows a more elegant solution please post it,
as I am getting headaches trying to think of one.)
Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net
More information about the Python-list
mailing list