Splitting a list

Discussion in 'Python' started by Ian Sparks, Aug 31, 2004.

  1. Ian Sparks

    Ian Sparks Guest

    string.split() is very useful, but what if I want to split a list of integers on some element value?

    e.g. :

    >> l = [1,2,3,-1,4,5,-1,8,9]
    >> l.split(-1)
    >> [[1,2,3],[4,5],[8,9]]


    Here's my hideous first pass :

    >> [[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in l]).split('-1')]
    >> [[1, 2, 3], [4, 5], [8, 9]]


    When I see code like that I just know I've missed something obvious....
     
    Ian Sparks, Aug 31, 2004
    #1
    1. Advertising

  2. In article <>,
    "Ian Sparks" <> wrote:

    > string.split() is very useful, but what if I want to split a list of integers
    > on some element value?
    >
    > e.g. :
    >
    > >> l = [1,2,3,-1,4,5,-1,8,9]
    > >> l.split(-1)
    > >> [[1,2,3],[4,5],[8,9]]

    >
    > Here's my hideous first pass :
    >
    > >> [[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in
    > >> l]).split('-1')]
    > >> [[1, 2, 3], [4, 5], [8, 9]]

    >
    > When I see code like that I just know I've missed something obvious....


    How about this?

    def split_list(L, brk):
    last = -1
    out = []

    for pos in [ x for (x, y) in enumerate(L) if y == brk ]:
    out.append(L[last + 1 : pos])
    last = pos

    out.append(L[last + 1:])
    return out

    -M

    --
    Michael J. Fromberger | Lecturer, Dept. of Computer Science
    http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
     
    Michael J. Fromberger, Aug 31, 2004
    #2
    1. Advertising

  3. Ian Sparks

    Paul McGuire Guest

    "Ian Sparks" <> wrote in message
    news:...
    string.split() is very useful, but what if I want to split a list of
    integers on some element value?

    e.g. :

    >> l = [1,2,3,-1,4,5,-1,8,9]
    >> l.split(-1)
    >> [[1,2,3],[4,5],[8,9]]


    Here's my hideous first pass :

    >> [[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in

    l]).split('-1')]
    >> [[1, 2, 3], [4, 5], [8, 9]]


    When I see code like that I just know I've missed something obvious....



    Maybe not the prettiest, but here's a solution using a generator (at least
    you're avoiding a bunch of conversions to/from string).

    -- Paul


    l = [1,2,3,-1,4,5,-1,8,9]

    def getSublists(lst, delim):
    loc = 0
    while loc < len(lst):
    try:
    nextloc = lst.index(delim,loc)
    except ValueError:
    nextloc = len(lst)
    yield lst[loc:nextloc]
    loc = nextloc+1

    print [ sub for sub in getSublists(l,-1) ]
     
    Paul McGuire, Aug 31, 2004
    #3
  4. def indices(x,y):
    if y in x:
    i = x.index(y)
    j = i+1
    return +[z+j for z in indices(x[j:],y)]
    return []

    def listSplit(x,y):
    z = [-1] + indices(x,y) + [len(x)]
    return [x[z+1:z[i+1]] for i in range(len(z)-1)]


    "Ian Sparks" <> wrote in message
    news:...
    string.split() is very useful, but what if I want to split a list of integers on
    some element value?

    e.g. :

    >> l = [1,2,3,-1,4,5,-1,8,9]
    >> l.split(-1)
    >> [[1,2,3],[4,5],[8,9]]


    Here's my hideous first pass :

    >> [[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in

    l]).split('-1')]
    >> [[1, 2, 3], [4, 5], [8, 9]]


    When I see code like that I just know I've missed something obvious....
     
    Elaine Jackson, Aug 31, 2004
    #4
  5. On Tue, 31 Aug 2004 09:54:17 -0400, "Ian Sparks"
    <> wrote:

    >string.split() is very useful, but what if I want to split a list of integers on some element value?


    Here are a few early-morning attempts :)

    def lsplit(L,sep):
    try:
    i = L.index(sep)
    return [L[:i]] + lsplit(L[i+1:],sep)
    except ValueError:
    return [L]

    def lsplit2(L,sep):
    i = 0
    res = []
    while True:
    try:
    j = L.index(sep,i)
    res.append(L[i:j])
    i = j+1
    except ValueError:
    res.append(L[i:])
    break
    return res

    def lsplit3(L,sep):
    i = 0
    while True:
    try:
    j = L.index(sep,i)
    yield L[i:j]
    i = j+1
    except ValueError:
    yield L[i:]
    break


    HTH
    Andrea
     
    Andrea Griffini, Sep 1, 2004
    #5
  6. Ian Sparks <> wrote:
    > string.split() is very useful, but what if I want to split a list of integers on some element value?
    >
    > e.g. :
    >
    > >> l = [1,2,3,-1,4,5,-1,8,9]
    > >> l.split(-1)
    > >> [[1,2,3],[4,5],[8,9]]


    reduce (lambda a, i: i == elem and (a + [[]]) or (a[:-1] + [a[-1]+]),
    l, [[]])

    martin
     
    Martin DeMello, Sep 1, 2004
    #6
  7. Ian Sparks <> wrote:

    > string.split() is very useful, but what if I want to split a list of

    integers on some element value?
    >
    > e.g. :
    >
    > >> l = [1,2,3,-1,4,5,-1,8,9]
    > >> l.split(-1)
    > >> [[1,2,3],[4,5],[8,9]]

    >
    > Here's my hideous first pass :
    >
    > >> [[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in

    l]).split('-1')]
    > >> [[1, 2, 3], [4, 5], [8, 9]]

    >
    > When I see code like that I just know I've missed something obvious....


    I think a simple generator might serve you well:

    def isplit(seq, separator):
    result = []
    for item in seq:
    if item == separator:
    yield result
    result = []
    else:
    result.append(item)
    yield result

    example use:

    >>> list(isplit([1,2,3,-1,4,5,-1,8,9], -1))

    [[1, 2, 3], [4, 5], [8, 9]]

    Note that, the way isplit is coded, seq can be any iterable, not just a
    list. You may not need this little extra generality, but it can't
    hurt...


    Alex
     
    Alex Martelli, Sep 1, 2004
    #7
    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:
    448
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    458
  3. John Dibling
    Replies:
    0
    Views:
    433
    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:
    497
    Peter Otten
    Oct 9, 2003
  5. Jeff Epler

    Re: Splitting a list

    Jeff Epler, Aug 31, 2004, in forum: Python
    Replies:
    2
    Views:
    297
    Peter Otten
    Aug 31, 2004
Loading...

Share This Page