pivot() equivalent

Discussion in 'Python' started by gundlach, Mar 11, 2010.

  1. gundlach

    gundlach Guest

    I *know* this already exists, but I can't remember where:

    def pivot(func, seq):
    # I know, a good implementation shouldn't call func() twice per item
    return ( (x for x in seq if func(x)), (x for x in seq if not
    func(x)) )

    I feel like I read a thread in which this was argued to death, and I
    can't find that either.

    The scenario: I have a sequence of lines from a file. I want to split
    it into those lines that contain a substring, and those that don't. I
    want it to be more efficient and prettier than

    with = [x for x in lines if substring in x]
    without = [x for x in lines if substring not in x]

    Does this exist?

    TIA,
    Michael
     
    gundlach, Mar 11, 2010
    #1
    1. Advertising

  2. gundlach

    John Posner Guest

    On 3/11/2010 6:16 PM, gundlach wrote:
    > I *know* this already exists, but I can't remember where:
    >
    > def pivot(func, seq):
    > # I know, a good implementation shouldn't call func() twice per item
    > return ( (x for x in seq if func(x)), (x for x in seq if not
    > func(x)) )
    >
    > I feel like I read a thread in which this was argued to death, and I
    > can't find that either.
    >
    > The scenario: I have a sequence of lines from a file. I want to split
    > it into those lines that contain a substring, and those that don't. I
    > want it to be more efficient and prettier than
    >
    > with = [x for x in lines if substring in x]
    > without = [x for x in lines if substring not in x]
    >
    > Does this exist?
    >
    > TIA,
    > Michael


    Try this:

    > type lines.txt

    the
    quick
    brown
    fox
    jumps
    over
    the
    lazy
    dog

    > type pivot.py


    def pivot(pivot_function, filename):
    a_list = []
    b_list = []
    for line in open(filename):
    target_list = a_list if pivot_function(line) else b_list
    target_list.append(line[:-1])
    return (a_list, b_list)

    print pivot(lambda line: 'e' in line, "lines.txt")

    > python pivot.py

    (['the', 'over', 'the'], ['quick', 'brown', 'fox', 'jumps', 'lazy', 'dog'])

    -John
     
    John Posner, Mar 11, 2010
    #2
    1. Advertising

  3. gundlach

    MRAB Guest

    gundlach wrote:
    > I *know* this already exists, but I can't remember where:
    >
    > def pivot(func, seq):
    > # I know, a good implementation shouldn't call func() twice per item
    > return ( (x for x in seq if func(x)), (x for x in seq if not
    > func(x)) )
    >
    > I feel like I read a thread in which this was argued to death, and I
    > can't find that either.
    >
    > The scenario: I have a sequence of lines from a file. I want to split
    > it into those lines that contain a substring, and those that don't. I
    > want it to be more efficient and prettier than
    >
    > with = [x for x in lines if substring in x]
    > without = [x for x in lines if substring not in x]
    >
    > Does this exist?
    >

    The clearest way is just:

    def pivot(func, seq):
    with, without = [], []
    for x in seq:
    if func(x):
    with.append(x)
    else:
    without.append(x)
    return with, without
     
    MRAB, Mar 11, 2010
    #3
    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. =?Utf-8?B?QWpheQ==?=

    Create Pivot table

    =?Utf-8?B?QWpheQ==?=, Feb 13, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    7,463
    Alvin Bruney [MVP]
    Feb 16, 2004
  2. =?Utf-8?B?UG9udGlNYXg=?=

    Pivot table control

    =?Utf-8?B?UG9udGlNYXg=?=, Jun 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,378
    Ken Cox [Microsoft MVP]
    Jun 15, 2004
  3. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: Pivot table control

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    369
    Phil Winstanley [Microsoft MVP ASP.NET]
    Jun 15, 2004
  4. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: Pivot table control

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    426
    Kevin Spencer
    Jun 15, 2004
  5. Grey

    Pivot table function in asp.net

    Grey, Sep 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    839
Loading...

Share This Page