List Question

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

How is this expressed in Python?

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

y is a Python list.

2. Paul HankinGuest

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.

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

3. Michael BentleyGuest

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
4. Paul HankinGuest

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.

>
> 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
5. Pablo ZilianiGuest

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

>
> 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
6. Paul McGuireGuest

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.

>
> 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
7. Paul McGuireGuest

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.

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

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
8. Bruno DesthuilliersGuest

> 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
9. Paul HankinGuest

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
10. Bjoern SchliessmannGuest

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