Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   List Question (http://www.velocityreviews.com/forums/t541533-list-question.html)

List Question

How is this expressed in Python?

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

y is a Python list.

 Paul Hankin 10-02-2007 09:20 PM

Re: List Question

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

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 10-02-2007 09:22 PM

Re: List Question

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 10-02-2007 09:23 PM

Re: List Question

On Oct 2, 10:20 pm, Paul Hankin <paul.han...@gmail.com> wrote:
> On Oct 2, 10:06 pm, brad <byte8b...@gmail.com> wrote:
>
> > How is this expressed in Python?

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

>
> > y is a Python list.

>
> 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 10-02-2007 09:58 PM

Re: List Question

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

>
> 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 10-02-2007 10:09 PM

Re: List Question

On Oct 2, 4:20 pm, Paul Hankin <paul.han...@gmail.com> wrote:
> On Oct 2, 10:06 pm, brad <byte8b...@gmail.com> wrote:
>
> > How is this expressed in Python?

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

>
> > y is a Python list.

>
> 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 10-02-2007 10:23 PM

Re: List Question

On Oct 2, 4:58 pm, Pablo Ziliani <pa...@decode.com.ar> wrote:
> Paul Hankin wrote:
> > On Oct 2, 10:06 pm, brad <byte8b...@gmail.com> wrote:

>
> >> How is this expressed in Python?

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

>
> >> y is a Python list.

>
> > 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 -

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 10-03-2007 06:49 AM

Re: List Question

> 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 10-03-2007 07:34 AM

Re: List Question

On Oct 2, 11:09 pm, Paul McGuire <pt...@austin.rr.com> wrote:
> On Oct 2, 4:20 pm, Paul Hankin <paul.han...@gmail.com> wrote:
> > On Oct 2, 10:06 pm, brad <byte8b...@gmail.com> 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 10-03-2007 11:51 AM

Re: List Question

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.

 All times are GMT. The time now is 09:04 PM.