interesting python functions

# send each member of range(5) sequence to p function which is simply printing it's argument

def p(x):
print x
map(p,range(5))

#filter sequence for given condition

seq=[2,22,22.2]
z=filter(lambda x:x>2, seq)
print z

#take first two argument of sequence as x,y then apply x*y , take result of this as a new first argument take 3rd argument which is 3 here as a new second argument and continue to process till the end of sequence

numbers=[1,2,3,4,5]
print reduce(lambda x,y:x*y,numbers)



here are the outputs of functions above
0
1
2
3
4

[22, 22.199999999999999]

120

--------------------------------------------------
Python Environment
--------------------------------------------------
version = 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)]
platform = win32

Comments

Popular posts from this blog

Pyppeteer fix for BrowserError: Browser closed unexpectedly

overlay filesystem and containers

How to add pagination to django comments for your model