Velocity Reviews > List Question

# List Question

brad
Guest
Posts: n/a

 10-02-2007
How is this expressed in Python?

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

y is a Python list.

Paul Hankin
Guest
Posts: n/a

 10-02-2007
On Oct 2, 10:06 pm, brad <(E-Mail Removed)> 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

Michael Bentley
Guest
Posts: n/a

 10-02-2007

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

Paul Hankin
Guest
Posts: n/a

 10-02-2007
On Oct 2, 10:20 pm, Paul Hankin <(E-Mail Removed)> wrote:
> On Oct 2, 10:06 pm, brad <(E-Mail Removed)> 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

Pablo Ziliani
Guest
Posts: n/a

 10-02-2007
Paul Hankin wrote:
> On Oct 2, 10:06 pm, brad <(E-Mail Removed)> 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[i] == x:
count += 1
i += 1
except RuntimeError:
pass
except IndexError:
pass
else:
print x

</joke>

Sorry, couldn't resist...

Paul McGuire
Guest
Posts: n/a

 10-02-2007
On Oct 2, 4:20 pm, Paul Hankin <(E-Mail Removed)> wrote:
> On Oct 2, 10:06 pm, brad <(E-Mail Removed)> 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
Guest
Posts: n/a

 10-02-2007
On Oct 2, 4:58 pm, Pablo Ziliani <(E-Mail Removed)> wrote:
> Paul Hankin wrote:
> > On Oct 2, 10:06 pm, brad <(E-Mail Removed)> 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[i] == 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

Bruno Desthuilliers
Guest
Posts: n/a

 10-03-2007
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

Paul Hankin
Guest
Posts: n/a

 10-03-2007
On Oct 2, 11:09 pm, Paul McGuire <(E-Mail Removed)> wrote:
> On Oct 2, 4:20 pm, Paul Hankin <(E-Mail Removed)> wrote:
> > On Oct 2, 10:06 pm, brad <(E-Mail Removed)> 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

Bjoern Schliessmann
Guest
Posts: n/a

 10-03-2007
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[i] == 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.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post asil klin Java 28 03-05-2011 01:59 AM OW Ghim Siong Python 2 11-30-2010 12:22 PM Debajit Adhikary Python 17 10-18-2007 06:45 PM dackz Python 0 02-06-2007 04:44 PM roopa C++ 6 08-27-2004 06:18 PM

Advertisments