def multi(x):
a=x*x
return a
r=map(multi,[1,2,3,4,5])
print(list(r))
from functools import reduce
def add(x,y):
a=x+y
return a
r=reduce(add,[1,2,3,4,5,6])
print(r)
def char2num(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
def num(x,y):
a=x*10+y
return a
print(reduce(num,map(char2num,'13579')))
def normalize(word):
first=word[0].upper()
rest=word[1:].lower()
return first+rest
L1=['adam', 'LISA', 'barT']
L2=list(map(normalize,L1))
print(L2)
def is_odd(n):
return n%2 == 1
print(list(filter(is_odd,[1,2,3,4,5,6,7])))
s=['Facebook;Google+;MySpace', 'Apple;Android']
s1=[ele.strip(' ').split(';') for ele in s]
print(s1)
def un_blank(s):
return s and s.strip()
print(list(filter(un_blank,['A', '', 'B', None, 'C', ' '])))
def _init():
n=1
while True:
n=n+2
yield n
def not_dividible(n):
return lambda x :x%n>0
def prime():
n=2
it=_init()
while True:
n=next(it)
yield n
it=filter(not_dividible(n),it)
for n in prime():
if n<600:
print(n)
else:
break
'''def _init():
n=1
while True:
n=n+1
yield n
def back(n):
n=str(n)
m=n[-1::-1]
return lambda n :n==m
def prime():
yield 1
it=_init()
while True:
n=next(it)
yield n
it=filter(back(n),it)
for n in prime():
if n < 500:
print(n)
else:
break
还是不清楚在哪里出错了
'''
def back(n):
n=str(n)
m=n[-1::-1]
if m==n:
return True
else:
return False
print(list(filter(back,range(12580,13580))))
print(sorted([36,5,-12,5,-21],key=abs))
print(sorted(['gakki','umr','toda','emma'],key=str.lower))
print(sorted(['gakki','umr','toda','emma'],key=str.lower,reverse=True))