Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Introduction to Python List Comprehensions
#5
map/filter equivalent:
Code:
sum = []
for x in range(1000):
    sum.append(x)
sum = filter(lambda x : x % 3 == 0 or x % 5 == 0, sum)
sum = reduce(lambda x, y: x + y, sum)
print sum
Yep. Your way is better ;)

Edit:
One liner FTW!
Code:
reduce(lambda x,y : x+y, [x for x in xrange(1000) if x % 3 == 0 or x % 5 == 0])
[Image: izsyo6.jpg]


Reply


Messages In This Thread
RE: Introduction to Python List Comprehensions - by uber1337 - 02-18-2010, 09:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Help Dεlluzion 3 1,793 09-30-2019, 12:59 AM
Last Post: samsmith001
  Simple Python Python Compiler Canoris 21 8,487 06-01-2011, 06:30 PM
Last Post: Filefinder
  Python 2 vs 3? Jake 8 2,272 12-11-2010, 04:13 PM
Last Post: Bursihido
  Python help Kharnage 2 768 02-12-2010, 09:07 PM
Last Post: Kharnage
  "==" and "is" in Python Canoris 1 756 02-07-2010, 03:55 PM
Last Post: uber1337

Forum Jump:


Users browsing this thread: 1 Guest(s)