python列表生成

L1=['Hello','World',18,'Apple',None]

L2=[s.lower() for s in L1 if isinstance(s,str)]

L2=[s.lower() if isinstance(s,str) else s for s in L1]

print(L2)

def fib(max):

n,a,b=0,0,1

while n < max:

yield b

a,b=b,a+b

n=n+1

return 'done'


for n in fib(6):

print(n)


g=fib(6)


while True:

try:

x=next(g)

print('g',x)

except StopIteration as e:

print('Generator return value',e.value)

break




from functools import reduce
DIGITS={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}

def str2int(s):
    def fn(x,y):
        return 10*x+y
    def char2num(s):
        return DIGITS[s]
    return reduce(fn,map(char2num,s))

print(str2int('123456'))


from functools import reduce
DIGITS={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}


def char2num(s):
    return DIGITS[s]

def str2int(s):
    return reduce(lambda x,y: x*10+y,map(char2num,s))

print(str2int('12567'))




请使用浏览器的分享功能分享到微信等