Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > easiest way to split a list into evenly divisble smaller lists, and assign them to variables?

Reply
Thread Tools

easiest way to split a list into evenly divisble smaller lists, and assign them to variables?

 
 
flamesrock
Guest
Posts: n/a
 
      06-06-2005
Lets say I have a list containing 12, 13, 23 or however many entries.
What I want is the greatest number of lists evenly divisible by a
certain number, and for those lists to be assigned to variables.

This leads to a few problems..

If I don't know the length of the list beforehand, I can't create the
variables and hardcode them. Is it possible in python to generate free
variables without stating them explicitly in the code, and if so, how
would you get a reference to them?

Secondly, is there an easier way to chop a list up into smaller lists?

consider this code:
# my_list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,
# 15,16,17,18,19,20,21,22,23,24,25]
# entry=0
# dictionary_of_lists = {}
# while len(original_list) != 0:
# new_list=[]
# for x in range(4):
# if len(original_list) > 0:
# new_list.append(files.pop(0))
# dictionary_of_lists[entry] = new_list
# entry +=1

would give us
{1:[1,2,3,4],2:[5,6,7,8],
3:[9,10,11,12],4:[13,14,15,16],
5:[17,18,19,20],6:[21,22,23,24],7:[25]}

Is there a better way? What I'd like to do is create free variables
without needing to put them in a dictionary.

If not, is it possible in other languages?

-thank in advance

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      06-06-2005
"flamesrock" <(E-Mail Removed)> writes:
> Lets say I have a list containing 12, 13, 23 or however many entries.
> What I want is the greatest number of lists evenly divisible by a
> certain number, and for those lists to be assigned to variables.


You almost certainly don't want to do that. That's something
beginning programmers often think of doing, but it's almost always
better to use something more general, like an array.

> consider this code:
> # my_list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,
> # 15,16,17,18,19,20,21,22,23,24,25]
> # entry=0
> # dictionary_of_lists = {}
> # while len(original_list) != 0:


Umm, what's "original_list"? Do you mean "my_list"? The rest of
your code has similar errors.

> would give us
> {1:[1,2,3,4],2:[5,6,7,8],
> 3:[9,10,11,12],4:[13,14,15,16],
> 5:[17,18,19,20],6:[21,22,23,24],7:[25]}
>
> Is there a better way? What I'd like to do is create free variables
> without needing to put them in a dictionary.


Your best bet is probably to create a list of lists:

sublist_length = 4 # desired length of the "inner" lists
list_of_lists = []
for i in xrange(0, len(my_list), sublist_length):
list_of_lists.append(my_list[i: i+sublist_length])

That gives list_of_lists =

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],
[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24], [25]]

So you'd just use list_of_lists[0] to refer to the 1st sublist, etc.
 
Reply With Quote
 
 
 
 
flamesrock
Guest
Posts: n/a
 
      06-06-2005
hmmm..that makes much more sense. thanks

 
Reply With Quote
 
gene tani
Guest
Posts: n/a
 
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to split one XML into smaller XML; received via a XMLHttpRequest? NorbertH Javascript 1 01-04-2007 01:30 AM
How to automatically split a single DSC-H1 multi-burst JPG into 16 smaller images Wayne Schnurr Digital Photography 1 03-18-2006 12:37 PM
How to split a large avi file into smaller avi by size Avner DVD Video 1 10-31-2005 06:34 PM
font of the active title bar appears smaller and smaller automatic =?Utf-8?B?dGhhbmt5b3U=?= Windows 64bit 1 10-12-2005 09:24 AM
DSLR, Smaller Sensor, Smaller TTL Viewfinder? Which has the largest? Geshu Iam Digital Photography 7 08-17-2004 06:52 PM



Advertisments