List

Gold Fish occeanlinux at linuxmail.org
Mon Jun 3 08:09:43 CEST 2002


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']]
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.




More information about the Python-list mailing list