List Question

Discussion in 'Python' started by brad, Oct 2, 2007.

  1. brad

    brad Guest

    How is this expressed in Python?

    If x is in y more than three times:
    print x

    y is a Python list.
     
    brad, Oct 2, 2007
    #1
    1. Advertising

  2. brad

    Paul Hankin Guest

    On Oct 2, 10:06 pm, brad <> wrote:
    > How is this expressed in Python?
    >
    > If x is in y more than three times:
    > print x
    >
    > y is a Python list.


    Simple and readable:
    if len([a for a in y if x == a]) > 3:
    print x

    Or the slightly-too-flashy version:
    if sum(1 for a in y if x == a) > 3:
    print x

    --
    Paul Hankin
     
    Paul Hankin, Oct 2, 2007
    #2
    1. Advertising

  3. On Oct 2, 2007, at 2:06 PM, brad wrote:

    > How is this expressed in Python?
    >
    > If x is in y more than three times:
    > print x
    >
    > y is a Python list.


    # Try using help -- help(list) or help(list.count) for instance...
    if y.count(x) > 3:
    print x
     
    Michael Bentley, Oct 2, 2007
    #3
  4. brad

    Paul Hankin Guest

    On Oct 2, 10:20 pm, Paul Hankin <> wrote:
    > On Oct 2, 10:06 pm, brad <> wrote:
    >
    > > How is this expressed in Python?

    >
    > > If x is in y more than three times:
    > > print x

    >
    > > y is a Python list.

    >
    > Simple and readable:
    > if len([a for a in y if x == a]) > 3:
    > print x
    >
    > Or the slightly-too-flashy version:
    > if sum(1 for a in y if x == a) > 3:
    > print x


    Or the embarrassingly simple:

    if y.count(x) > 3:
    print x

    --
    Paul Hankin
     
    Paul Hankin, Oct 2, 2007
    #4
  5. Paul Hankin wrote:
    > On Oct 2, 10:06 pm, brad <> wrote:
    >
    >> How is this expressed in Python?
    >>
    >> If x is in y more than three times:
    >> print x
    >>
    >> y is a Python list.
    >>

    >
    > Simple and readable:
    > if len([a for a in y if x == a]) > 3:
    > print x
    >
    > Or the slightly-too-flashy version:
    > if sum(1 for a in y if x == a) > 3:
    > print x


    <joke>

    I always use this full-featured, all-inclusive, rock-solid version (see
    the try/except block):

    count = i = 0
    x = 1
    y = [1,2,3,4,5,1,2,3,4,1,2,1]
    try:
    while count < 3:
    if y == x:
    count += 1
    i += 1
    except RuntimeError:
    pass
    except IndexError:
    pass
    else:
    print x

    </joke>


    Sorry, couldn't resist...
     
    Pablo Ziliani, Oct 2, 2007
    #5
  6. brad

    Paul McGuire Guest

    On Oct 2, 4:20 pm, Paul Hankin <> wrote:
    > On Oct 2, 10:06 pm, brad <> wrote:
    >
    > > How is this expressed in Python?

    >
    > > If x is in y more than three times:
    > > print x

    >
    > > y is a Python list.

    >
    > Simple and readable:
    > if len([a for a in y if x == a]) > 3:
    > print x
    >
    > Or the slightly-too-flashy version:
    > if sum(1 for a in y if x == a) > 3:
    > print x
    >
    > --
    > Paul Hankin


    As long as you are eschewing count for sum, don't forget that true is
    1 and false is 0:

    if sum(x==a for a in y) > 3:
    print x

    -- Paul
     
    Paul McGuire, Oct 2, 2007
    #6
  7. brad

    Paul McGuire Guest

    On Oct 2, 4:58 pm, Pablo Ziliani <> wrote:
    > Paul Hankin wrote:
    > > On Oct 2, 10:06 pm, brad <> wrote:

    >
    > >> How is this expressed in Python?

    >
    > >> If x is in y more than three times:
    > >> print x

    >
    > >> y is a Python list.

    >
    > > Simple and readable:
    > > if len([a for a in y if x == a]) > 3:
    > > print x

    >
    > > Or the slightly-too-flashy version:
    > > if sum(1 for a in y if x == a) > 3:
    > > print x

    >
    > <joke>
    >
    > I always use this full-featured, all-inclusive, rock-solid version (see
    > the try/except block):
    >
    > count = i = 0
    > x = 1
    > y = [1,2,3,4,5,1,2,3,4,1,2,1]
    > try:
    > while count < 3:
    > if y == x:
    > count += 1
    > i += 1
    > except RuntimeError:
    > pass
    > except IndexError:
    > pass
    > else:
    > print x
    >
    > </joke>
    >
    > Sorry, couldn't resist...- Hide quoted text -
    >
    > - Show quoted text -


    Well, there is an advantage to your method/madness, in that it does
    short-circuiting once the magic count of 3 is found. If the list
    contained *many* entries, or if the predicate were expensive to
    evaluate, or if the count were likely to be satisfied within the first
    few list elements, your approach beats the other count or sum
    suggestions (since they evaluate all list entries).

    Here's a version of your code using itertools.takewhile:

    count = 0
    for a in itertools.takewhile(lambda _:count<3,y):
    count += (x==a)
    if count==3:
    print x

    -- Paul
     
    Paul McGuire, Oct 2, 2007
    #7
  8. brad a écrit :
    > How is this expressed in Python?
    >
    > If x is in y more than three times:
    > print x
    >
    > y is a Python list.



    if y.count(x) > 3:
    print x
     
    Bruno Desthuilliers, Oct 3, 2007
    #8
  9. brad

    Paul Hankin Guest

    On Oct 2, 11:09 pm, Paul McGuire <> wrote:
    > On Oct 2, 4:20 pm, Paul Hankin <> wrote:
    > > On Oct 2, 10:06 pm, brad <> wrote:

    >
    > > > How is this expressed in Python?

    >
    > > > If x is in y more than three times:
    > > > print x

    >
    > > > y is a Python list.

    >
    > > Or the slightly-too-flashy version:
    > > if sum(1 for a in y if x == a) > 3:
    > > print x

    >
    > As long as you are eschewing count for sum, don't forget that true is
    > 1 and false is 0:
    >
    > if sum(x==a for a in y) > 3:
    > print x


    I like it!

    --
    Paul Hankin
     
    Paul Hankin, Oct 3, 2007
    #9
  10. Pablo Ziliani wrote:

    > <joke>
    >
    > I always use this full-featured, all-inclusive, rock-solid version
    > (see the try/except block):
    >
    > count = i = 0
    > x = 1
    > y = [1,2,3,4,5,1,2,3,4,1,2,1]
    > try:
    > while count < 3:
    > if y == x:
    > count += 1
    > i += 1
    > except RuntimeError:
    > pass
    > except IndexError:
    > pass
    > else:
    > print x
    >
    > </joke>


    Wrong, this must be just

    except:
    pass

    Regards&CNRE,


    Björn

    --
    BOFH excuse #141:

    disks spinning backwards - toggle the hemisphere jumper.
     
    Bjoern Schliessmann, Oct 3, 2007
    #10
    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. roopa
    Replies:
    6
    Views:
    766
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    497
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    699
    Debajit Adhikary
    Oct 18, 2007
  4. OW Ghim Siong
    Replies:
    2
    Views:
    405
    Peter Otten
    Nov 30, 2010
  5. asil klin
    Replies:
    28
    Views:
    1,315
    Andreas Leitgeb
    Mar 5, 2011
Loading...

Share This Page