v=['A','D','E','N'] e=enumerate(v) print e # a=list(enumerate(v)) b=zip(xrange(len(v)),v) print a==b # True # Infatti: print a # [(0,'A'), (1,'D'), (2,'E'), (3,'N')] print b # [(0,'A'), (1,'D'), (2,'E'), (3,'N')] --------------------------------------- v=[8,13,0,5,17,8,6,24,6,15,3] s=0 for i,x in enumerate(v): if x>10: s+=i print s # 21, perche' sono maggiori di 10 # gli elementi con gli indici # 1,4,7,9 e 1+4+7+9=21. --------------------------------------- def sommatrigonometrica (a,x): s=0 for n,an in enumerate(a): s+=an*math.cos(n*x) return s a=[2,3,0,4,7,1,3] print sommatrigonometrica(a,0.2) # 14.7458647279 --------------------------------------- a='ABCDabcd' print map(ord,a) # [65, 66, 67, 68, 97, 98, 99, 100] --------------------------------------- u=map(lambda x: x**2, xrange(10)) print u # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] --------------------------------------- def vocale (x): return x.lower() in 'aeiou' print map(vocale,'Crema') # [False, False, True, False, True] --------------------------------------- def grafico (f,a): return zip(a,map(f,a)) a=(0,1,2,3) print grafico(lambda x: x**4,a) # [(0, 0), (1, 1), (2, 16), (3, 81)] --------------------------------------- a=(88,20,17,4,58) v=map(str,a) print v # ['88', '20', '17', '4', '58'] print ' '.join(v) # 88 20 17 4 58 --------------------------------------- def diagonale (a,n=2): return map(lambda x: (x,)*n,a) # Non dimenticare la virgola. a=(2,3,5,6) print diagonale(a) # [(2, 2), (3, 3), (5, 5), (6, 6)] --------------------------------------- def dnanumerico (x): x=x.lower() sost={'g': 0, 'a': 1, 'c': 2, 't': 3} return sost[x] g='TGAATGCTAC' print map(dnanumerico,g) # [3, 0, 1, 1, 3, 0, 2, 3, 1, 2] --------------------------------------- def cesare (a): o=ord('A') def codifica(x): n=(ord(x)-o+3)%26 return chr(o+n) v=map(codifica,a) return ''.join(v) a='CRASCASTRAMOVEBO' print cesare(a) # FUDVFDVWUDPRYHER --------------------------------------- a=map(lambda i: map (lambda j: i==j,xrange(4)), xrange(4)) for x in a: print x --------------------------------------- a=map(lambda i: map (lambda j: (i==j)+0,xrange(4)), xrange(4)) for x in a: print x --------------------------------------- def ipercubo (m): def rapp(x): return rapp2(x,cifre=m) return map(rapp,xrange(2**m)) for x in ipercubo(4): print x # Output: