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

Discussion in 'Python' started by flamesrock, Jun 6, 2005.

  1. flamesrock

    flamesrock Guest

    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
     
    flamesrock, Jun 6, 2005
    #1
    1. Advertising

  2. flamesrock

    Paul Rubin Guest

    "flamesrock" <> 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.
     
    Paul Rubin, Jun 6, 2005
    #2
    1. Advertising

  3. flamesrock

    flamesrock Guest

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

    hmmm..that makes much more sense. thanks :)
     
    flamesrock, Jun 6, 2005
    #3
  4. flamesrock

    gene tani Guest

    gene tani, Jun 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg N.
    Replies:
    8
    Views:
    21,514
    Spartanicus
    May 17, 2005
  2. Bart Nessux

    breaking a list into smaller lists

    Bart Nessux, Jun 17, 2004, in forum: Python
    Replies:
    7
    Views:
    599
    Reinhold Birkenfeld
    Jun 17, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    441
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Daniel Nogradi
    Replies:
    3
    Views:
    370
    Dennis Lee Bieber
    Nov 10, 2006
  5. NorbertH
    Replies:
    1
    Views:
    97
Loading...

Share This Page