List Question

B

brad

How is this expressed in Python?

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

y is a Python list.
 
P

Paul Hankin

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
 
M

Michael Bentley

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
 
P

Pablo Ziliani

Paul said:
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...
 
P

Paul McGuire

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

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
 
P

Paul McGuire

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
 
B

Bruno Desthuilliers

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
 
B

Bjoern Schliessmann

Pablo said:
<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top