首页 > 编程语言 > 详细

python函数式编程,列表生成式

时间:2017-05-26 10:29:55      阅读:305      评论:0      收藏:0      [点我收藏+]

1.python 中常见的集中存储数据的结构:

  列表

  集合

  字典

  元组

  字符串

  双队列

  堆

其中最常见的就是列表,字典。

2.下面讲一些运用循环获取字典列表的元素

1 >>> dic={name:zhangsan,age:24,city:jinhua}
2 >>> for key,value in dic.items():
3     print(key,value)
4 
5     
6 name zhangsan
7 age 24
8 city jinhua

循环获取列表

>>> lists=[1,2,3,4,5]
>>> for item in lists:
    item+1

    
2
3
4
5
6

3.python函数式编程的一些介绍

Python关于函数编程的一些函数有:

  map(function,list),映射函数

  filter(),过滤函数

  reduce(),规约函数

  lambda函数

  列表生成式

1 >>> def inc(x):return x+1
2 >>> list(map(inc,lists))
3 [2, 3, 4, 5, 6]
4 将函数用lambda表达式,缩写为一行代码
5 >>> items=[1,2,3,4]
6 >>> list(map((lambda x:x+1),items))
7 [2, 3, 4, 5]
filter函数的使用
>>> list(filter((lambda x:x<3),items))
[1, 2]
1 reduce函数需要导入reduce模块
2 >>> from functools import reduce
3 >>> reduce((lambda x,y:x/y),items)
4 0.041666666666666664
5 函数式标称的最后一个概念是列表生成式
6 >>> s=[x**2 for x in range(3)]
7 >>> s
8 [0, 1, 4]

 

python函数式编程,列表生成式

原文:http://www.cnblogs.com/caojunjie/p/6906897.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!