B
brad
How is this expressed in Python?
If x is in y more than three times:
print x
y is a Python list.
If x is in y more than three times:
print x
y is a Python list.
How is this expressed in Python?
If x is in y more than three times:
print x
y is a Python list.
How is this expressed in Python?
If x is in y more than three times:
print x
y is a Python list.
How is this expressed in Python?If x is in y more than three times:
print xy 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 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
How is this expressed in Python?If x is in y more than three times:
print xy 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
Simple and readable:
if len([a for a in y if x == a]) > 3:
print xOr 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 -
How is this expressed in Python?
If x is in y more than three times:
print x
y is a Python list.
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
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>
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.