List Splitting

Discussion in 'Python' started by Steven, Aug 21, 2006.

  1. Steven

    Steven Guest

    Hello everyone,

    I'm trying to work through a bit of a logic issue I'm having with a
    script I'm writing. Essentially, I have a list that's returned to
    me from another command that I need to regroup based on some aribitrary
    length.

    For the purposes of this question, the list will be:

    t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]

    Now, I know that every 3rd element of the list belongs together:

    Group 1 = 0, 3, 6
    Group 2 = 1, 4, 7
    Group 3 = 2, 5, 8

    I'm trying to sort this list out so that I get a list of lists that
    contain the correct elements:

    Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
    ["c", "a", "t" ] ]

    The actual data isn't as simple as this, but if I can get the logic
    sorted out, I can handle the other part.

    Anyone have any good ideas on how to do this?
    Steven, Aug 21, 2006
    #1
    1. Advertising

  2. Steven skrev:

    > For the purposes of this question, the list will be:
    >
    > t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
    >
    > Now, I know that every 3rd element of the list belongs together:
    >
    > Group 1 = 0, 3, 6
    > Group 2 = 1, 4, 7
    > Group 3 = 2, 5, 8
    >
    > I'm trying to sort this list out so that I get a list of lists
    > that contain the correct elements:
    >
    > Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
    > ["c", "a", "t" ] ]


    #v+

    >>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
    >>> [t[i::3] for i in range(3)]

    [['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]
    >>>


    #v-

    Cheers,

    --
    Klaus Alexander Seistrup
    SubZeroNet, Copenhagen, Denmark
    http://magnetic-ink.dk/
    Klaus Alexander Seistrup, Aug 21, 2006
    #2
    1. Advertising

  3. Steven

    Bill Pursell Guest

    Steven wrote:
    > Hello everyone,
    >
    > I'm trying to work through a bit of a logic issue I'm having with a
    > script I'm writing. Essentially, I have a list that's returned to
    > me from another command that I need to regroup based on some aribitrary
    > length.
    >
    > For the purposes of this question, the list will be:
    >
    > t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
    >
    > Now, I know that every 3rd element of the list belongs together:
    >
    > Group 1 = 0, 3, 6
    > Group 2 = 1, 4, 7
    > Group 3 = 2, 5, 8
    >
    > I'm trying to sort this list out so that I get a list of lists that
    > contain the correct elements:
    >
    > Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
    > ["c", "a", "t" ] ]
    >
    > The actual data isn't as simple as this, but if I can get the logic
    > sorted out, I can handle the other part.
    >
    > Anyone have any good ideas on how to do this?


    how about:
    >>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
    >>> [t[i::3] for i in range(0,len(t)/3)]

    [['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]
    --
    Bill Pursell
    Bill Pursell, Aug 21, 2006
    #3
  4. Steven

    Tim Chase Guest

    > For the purposes of this question, the list will be:
    >
    > t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
    >
    > Now, I know that every 3rd element of the list belongs together:
    >
    > Group 1 = 0, 3, 6
    > Group 2 = 1, 4, 7
    > Group 3 = 2, 5, 8
    >
    > I'm trying to sort this list out so that I get a list of lists that
    > contain the correct elements:
    >
    > Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
    > ["c", "a", "t" ] ]


    Well, the following worked for me:

    >>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
    >>> stride = 3
    >>> Goal = [t[i::stride] for i in range(stride)]
    >>> Goal

    [['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]


    Or, if you like, in this example:

    >>> [''.join(t[i::stride]) for i in range(stride)]

    ['ant', 'bat', 'cat']

    if that's of any use.

    -tkc
    Tim Chase, Aug 21, 2006
    #4
  5. Steven

    Steven Guest

    Klaus Alexander Seistrup wrote:
    > >>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
    > >>> [t[i::3] for i in range(3)]

    > [['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]


    Klaus,

    Thanks for the fast reply! Had I taken the time to look at the
    list-type docs (which I did to understand how you were spliting the
    list), I'd probably have seen the slicing with step option. Another
    RTFM issue for me.

    Thanks again,
    Steven
    Steven, Aug 21, 2006
    #5
  6. Steven

    Neil Cerutti Guest

    On 2006-08-21, Steven <> wrote:
    > Hello everyone,
    >
    > I'm trying to work through a bit of a logic issue I'm having with a
    > script I'm writing. Essentially, I have a list that's returned to
    > me from another command that I need to regroup based on some aribitrary
    > length.
    >
    > For the purposes of this question, the list will be:
    >
    > t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
    >
    > Now, I know that every 3rd element of the list belongs together:
    >
    > Group 1 = 0, 3, 6
    > Group 2 = 1, 4, 7
    > Group 3 = 2, 5, 8


    from itertools import islice

    grouped = []
    grouped.append(list(islice(t, 0, None, 3))
    grouped.append(list(islice(t, 1, None, 3))
    grouped.append(list(islice(t, 2, None, 3))
    grouped.sort()

    This can probably be simplified and generalized, but I'm a novice, and
    that's a start.

    --
    Neil Cerutti
    Neil Cerutti, Aug 21, 2006
    #6
    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. John Ericson
    Replies:
    0
    Views:
    417
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    428
  3. John Dibling
    Replies:
    0
    Views:
    399
    John Dibling
    Jul 19, 2003
  4. Rajarshi Guha

    splitting a list into n groups

    Rajarshi Guha, Oct 8, 2003, in forum: Python
    Replies:
    13
    Views:
    475
    Peter Otten
    Oct 9, 2003
  5. Ian Sparks

    Splitting a list

    Ian Sparks, Aug 31, 2004, in forum: Python
    Replies:
    6
    Views:
    346
    Alex Martelli
    Sep 1, 2004
Loading...

Share This Page