def somma (*a): # Come a pagina 2. s=0 for x in a: s+=x return s print map(somma, (0,1,5,4),(2,0,3,8),(6,2,2,7)) # [8, 3, 10, 19] --------------------------------------- print map(None,(1,2,3)) # [1, 2, 3] print map(None,(11,12,13),(21,22,23)) # [(11, 21), (12, 22), (13, 23)] v=map(None, (11,12,13),(21,22,23),(31,32,33)) for r in v: print r --------------------------------------- a=(2,3,5,0,2); b=(1,3,8,0,4) def f(x,y): return (x+y,x-y) for x in map(f,a,b): print x, # (3,1) (6,0) (13,-3) (0,0) (6,-2) --------------------------------------- a=('Roma','Pisa','Milano','Trento') print [x[0] for x in a] # ['R', 'P', 'M', 'T'] a=[x*x+3 for x in xrange(8)] print a # [3, 4, 7, 12, 19, 28, 39, 52] a=[x+y for x,y in ((3,2),(5,6),(1,9))] print a # [5, 11, 10] --------------------------------------- def rapp2pv (n): # Prima versione. if n<=1: v=[n] else: v=rapp2pv(n/2) if n%2==0: v.append(0) else: v.append(1) return v --------------------------------------- def rapp2 (n,cifre=0): if n<=1: v=[n] else: v=rapp2(n/2) if n%2==0: v.append(0) else: v.append(1) d=cifre-len(v) if d>0: v=[0]*d+v return v --------------------------------------- def strdalista (a,sep=' '): return sep.join(map(str,a)) for n in range(13)+ \ [19,48,77,106,135,164,194,221]: print "%3d %s" \ %(n,strdalista(rapp2(n,cifre=8))) --------------------------------------- for n in xrange(256): print "%3d %s" \ %(n,strdalista(rapp2(n,cifre=8)))