统计一共有几个数字

 

s="sdfa45ads46723"

 

#lambda

 

>>> filter(lambda x:x.isdigit(),list(s))

['4', '5', '4', '6', '7', '2', '3']

>>> len(filter(lambda x:x.isdigit(),list(s)))

7

 

>>> reduce(lambda x,y:x+y,map(lambda x:x.isdigit(),list("sdfa45ads46723")))

7

 

>>> reduce(lambda x,y:x+y,map(lambda x:len(x),filter(lambda x:x.isdigit(),[i for i in s][::-1])))

7

 

列表的切片还是列表,如果取一个元素就是元素本身类型

>>> a=[1,2]

>>> a[0]

1

>>> a[0:1]

[1]

 

Map的作用

将每个列表元素都按表达式分别运算

 

 

Reduce()累加操作

>>> reduce(lambda x,y:x+y,[1,2,3])

6

X=1,y=2

结果3传给y

X从第二次开始存结果

 

 

reduce(lambda x,y:x+y+y,[1,2,3])

x=1,y=2,y=2
x=5,y=3,y=3
11
x是5就对了

>>> reduce(lambda x,y:x+x+y,[1,2,3])

1+1+2=4 

4+4+3=11

相关文章:

  • 2022-02-26
  • 2021-07-19
  • 2021-08-03
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
  • 2021-09-09
  • 2021-07-20
猜你喜欢
  • 2021-08-08
  • 2021-12-24
  • 2022-02-28
  • 2022-12-23
  • 2021-06-29
  • 2021-10-04
  • 2021-07-01
相关资源
相似解决方案