List

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Mon Jun 3 08:28:37 EDT 2002


----- Original Message -----
From: "Gold Fish" <occeanlinux at linuxmail.org>


> Mike C. Fletcher wrote:
>
> > I don't know if you'll consider this a more elegant way to divide up the
> > lists, but I think it's a little more beautiful than the index-mucking
> > approaches IMO:
> >
> > def into( source, count=3 ):
> >      source = source[:]
> >      return intoDestructive( source, count )
> >
> > def intoDestructive( source, count=3 ):
> >      if source and not count:
> >          raise ValueError( """Cannot divide into 0-length
sub-elements"""
> >          )
> >      elif not source:
> >          return []
> >      results = []
> >      if count > 0:
> >          while source:
> >              results.append( source[:count])
> >              del source[:count]
> >      else:
> >          while source:
> >              results.append( source[count:])
> >              del source[count:]
> >      return results
> >
> > It will be very slow for very large lists with positive slice values
> > (something the index-mucking approach doesn't suffer from), but for most
> > reasonably sized lists, should be fairly good.  It handles negative
> > steps as well, so you can slice up the list from the right _or_ the left
> > side :) .
> >
> > Enjoy all,
> > Mike
> >
> > Chris Gonnerman wrote:
> > ...
> >>
> >>     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.)
> > ...
> > _______________________________________
> >    Mike C. Fletcher
> >    http://members.rogers.com/mcfletch/
>
> Sorry for the very stupid question but i still don't get my goal, that's
is
> using Mike C. Fletcher method i will generate the list of elements again
> and don't know what size it is. For example :
> [['p1','p2','p3'],['p4','p5','p6'],['p7','p8','p9'],['p10']]

Eh?  What size it is?  len(Lists) answers that question.

> I want to seperate this list again in order to using them later .It look
> like you go to the realestate agent website and find the list of property
> then you just want to display 3 properties per pages. And you can go to
> other pages which also contain 3 properties as well until the last pages
> which has only 1 items. For example:
> Page 1: ['p1','p2','p3']
> Page 2: ['p4','p5','p6']
> Page 3: ['p7','p8','p9']
> Page 4: ['p10']
> I didn't sleep well due to this problem Can any help me.

Maybe if we understood the problem better.  The solutions given by both Mike
and myself break the main list down into sublists; we stored them in an
"outer" list because, well, they have to be stored SOMEWHERE.

Are you envisioning:

    list1 = ['p1','p2','p3']
    list2 = ['p4','p5','p6']
    list3 = ['p7','p8','p9']
    list4 = ['p10']

that is, each sublist has its own variable name?  It can be done but it is
almost always the wrong way.  Using the methods we gave above, the result is
(in effect):

    List[1] = ['p1','p2','p3']
    List[2] = ['p4','p5','p6']
    List[3] = ['p7','p8','p9']
    List[4] = ['p10']

which is basically equivalent to what I think you are trying to do.

Could you maybe explain the *entire* project?  Or do you, like, work for the
CIA?  (I'd rather you not tell me if you then have to kill me.)

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net







More information about the Python-list mailing list