zoukankan      html  css  js  c++  java
  • python函数式编程,列表生成式

    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]
  • 相关阅读:
    word 改造成html表单
    ceshi
    easyUI 多功能datagrid 用户控件
    easyUI 多功能datagrid
    实现easyUI+.net 商品管理的用户控件
    通过sql 实现简单分页(not in)
    Jquery 插件开发
    Hello Swift
    关于const_cast转换
    undefined reference to '__android_log_print'.
  • 原文地址:https://www.cnblogs.com/caojunjie/p/6906897.html
Copyright © 2011-2022 走看看