472,110 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

advanced listcomprehenions?

I am wondering if it is possible to write advanced listcomprehensions.

For example:
"""Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz"."""
Obv it doesnt have to be a list according tot hat definition but
suppose i want to generate that list.
>>[["Fizzbuzz",x] for x in xrange(1,101) if x%3 == 0 and x%5 == 0]
[['Fizzbuzz', 15], ['Fizzbuzz', 30], ['Fizzbuzz', 45], ['Fizzbuzz',
60], ['Fizzbuzz', 75], ['Fizzbuzz', 90]]

is not what i want. the following does the trick but is ldo not a
listcomprehension:

for i in xrange(1,101):
s = ""
if i%3 == 0:
s += "Fizz"
if i%5 == 0:
s += "Buzz"
if s:
print "%d : %s" % (i,s)
else:
print i

or to generate a lisrt but not by listcomprehsnion:
map(lambda x: (not x%3 and not x%5 and "FizzBuzz") or (not x%3 and
"Fizz")
or (not x%5 and "Buzz") or x, xrange(1,101))
Jun 27 '08 #1
6 1064
En Wed, 18 Jun 2008 18:42:00 -0300, cirfu <ci**********@yahoo.seescribió:
I am wondering if it is possible to write advanced listcomprehensions.

For example:
"""Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz"."""
Obv it doesnt have to be a list according tot hat definition but
suppose i want to generate that list.
Go to http://groups.google.com/group/comp.lang.python and search for "fizz
buzz"...
or to generate a lisrt but not by listcomprehsnion:
map(lambda x: (not x%3 and not x%5 and "FizzBuzz") or (not x%3 and
"Fizz")
or (not x%5 and "Buzz") or x, xrange(1,101))
You can translate that into a list comprehension - in general, map(f,
items) is the same as [f(x) for x in items]. We have then:

[(not x%3 and not x%5 and "FizzBuzz") or (not x%3 and "Fizz") or (not x%5
and "Buzz") or x for x in xrange(1,101)]

Quite unreadable IMHO. Just to add another variant to the zillion ones
already posted:

def fb(x):
mult3 = x%3 == 0
mult5 = x%5 == 0
if mult3 and mult5: return "FizzBuzz"
elif mult3: return "Fizz"
elif mult5: return "Buzz"
return str(x)

[fb(x) for x in range(1,101)]

--
Gabriel Genellina

Jun 27 '08 #2
On Jun 18, 10:42*pm, cirfu <circularf...@yahoo.sewrote:
I am wondering if it is possible to write advanced listcomprehensions.

For example:
"""Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz"."""
Obv it doesnt have to be a list according tot hat definition but
suppose i want to generate that list.
>[["Fizzbuzz",x] for x in xrange(1,101) if x%3 == 0 and x%5 ==0]

[['Fizzbuzz', 15], ['Fizzbuzz', 30], ['Fizzbuzz', 45], ['Fizzbuzz',
60], ['Fizzbuzz', 75], ['Fizzbuzz', 90]]

is not what i want. the following does the trick but is ldo not a
listcomprehension:

for i in xrange(1,101):
* * s = ""
* * if i%3 == 0:
* * * * s += "Fizz"
* * if i%5 == 0:
* * * * s += "Buzz"
* * if s:
* * * * print "%d : %s" % (i,s)
* * else:
* * * * print i

or to generate a lisrt but not by listcomprehsnion:
map(lambda x: (not x%3 and not x%5 and "FizzBuzz") or (not x%3 and
"Fizz")
or (not x%5 and "Buzz") or x, xrange(1,101))
[("FizzBuzz" if n % 15 == 0 else "Fizz" if n % 3 == 0 else "Buzz" if n
% 5 == 0 else str(n)) for n in range(1, 101)]
Jun 27 '08 #3
On Jun 18, 4:42*pm, cirfu <circularf...@yahoo.sewrote:
I am wondering if it is possible to write advanced listcomprehensions.

For example:
"""Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz"."""
Obv it doesnt have to be a list according tot hat definition but
suppose i want to generate that list.
>[["Fizzbuzz",x] for x in xrange(1,101) if x%3 == 0 and x%5 ==0]

[['Fizzbuzz', 15], ['Fizzbuzz', 30], ['Fizzbuzz', 45], ['Fizzbuzz',
60], ['Fizzbuzz', 75], ['Fizzbuzz', 90]]

is not what i want. the following does the trick but is ldo not a
listcomprehension:

for i in xrange(1,101):
* * s = ""
* * if i%3 == 0:
* * * * s += "Fizz"
* * if i%5 == 0:
* * * * s += "Buzz"
* * if s:
* * * * print "%d : %s" % (i,s)
* * else:
* * * * print i

or to generate a lisrt but not by listcomprehsnion:
map(lambda x: (not x%3 and not x%5 and "FizzBuzz") or (not x%3 and
"Fizz")
or (not x%5 and "Buzz") or x, xrange(1,101))
[(('Fizz' if num % 3 == 0 else '') + ('Buzz' if num % 5 == 0 else ''))
or str(num) for num in xrange(1, 101)]
Jun 27 '08 #4
Gabriel Genellina <ga*******@yahoo.com.arwrote:
[(not x%3 and not x%5 and "FizzBuzz") or (not x%3 and "Fizz") or (not x%5
and "Buzz") or x for x in xrange(1,101)]
Rather unpleasant. Note that a number is zero mod both 3 and 5 if and
only if it's zero mod 15. But we can do better.

A simple application of Fermat's Little Theorem (to distinguish units
mod 3 and 5 from non-units) gives us this:

[['FizzBuzz', 'Buzz', 'Fizz', False][pow(i, 2, 3) + 2*pow(i, 4, 5)] or
str(i) for i in xrange(1, 101)]

This is still inelegant, though. We can glue the results mod 3 and 5
together using the Chinese Remainder Theorem and working mod 15
instead. For example,

[['Fizz', 'FizzBuzz', False, None, 'Buzz'][(pow(i, 4, 15) + 1)%7] or
str(i) for i in xrange(1, 101)]

(A less mathematical approach would just use i%15 to index a table. But
that's not interesting. ;-) )

-- [mdw]
Jun 27 '08 #5
Duncan Booth <du**********@invalid.invalidwrote:
[['Fizz', 'Buzz', 'FizzBuzz', str(i)][62/(pow(i, 4, 15) + 1)%4] for i in
xrange(1, 101)]
Cute! ;-)

-- [mdw]
Jun 27 '08 #6
Terry Reedy <tj*****@udel.eduwrote:
The lookup table is a constant. If made a tuple, it will be compiled as
a constant (as least in 2.6, maybe 2.5).
Force of habit. I tend to work on lists by indexing and/or iterating,
and on tuples by destructuring, and choose types based on the kinds of
things I'll be doing. But I did intentionally ensure that the tables
were constant so that readers could apply the obvious optimization if
they wanted. (Also, unnecessarily computing str(i) seemed bad.)
In any case, it could (and to me should) be lifted out of the string
comp.
For performance, yes. But doing a modexp is going to kill performance
anyway, so I decided to save screen lines. After all, applying even
fairly basic number theory to a problem like this isn't really what one
might call a readable solution. ;-)

-- [mdw]
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Millissa | last post: by
10 posts views Thread by John Wells | last post: by
9 posts views Thread by John Robert | last post: by
11 posts views Thread by vdrab | last post: by
reply views Thread by Andrew Meador - ASCPA, MCSE, MCP+I, Network+, A+ | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.