zoukankan      html  css  js  c++  java
  • python语法—命名元祖、偏函数

    一、命名元祖

    from collections import namedtuple  # 导入namedtuple函数
    
    # 设定命名元组,含有name、age、gender三个信息,并给到student_info这个对象,通常做法会把info_tuple这一处设置为和student_info这一处一样的名字
    student_info = namedtuple("info_tuple", ['name', 'age', 'gender'])
    tu = student_info('2chun', 18, '')
    print(tu)
    # 得到:info_tuple(name='2chun',age=18,gender='男')
    print(tu.name)
    # 得到:2chun

     

    二、偏函数

    偏函数的功能就是可将某个函数中的某些部分固定不动

    例子:过滤出大于5的数据

    li1 = [1, 2, 10]
    li2 = [1, 19, 10]
    li3 = [22, 28, 10]

    res1=filter(lambda x:x>5,li1)

    res2=filter(lambda x:x>5,li2)

    res3=filter(lambda x:x>5,li3)

    等效于:

    from functools import partial

    filter2=partial(filter, lambda x:x>5)

    res1=filter2(li1)

    res2=filter2(li2)

    res3=filter2(li3)

  • 相关阅读:
    ORM之F和Q
    ORM查询
    Django
    jQuery基础
    DOM和BOM
    saas baas paas iaas 的理解
    分布式架构的演进过程
    tomcat 配置https 证书
    idea 学习总结
    简单数据库连接池-总结
  • 原文地址:https://www.cnblogs.com/erchun/p/14678184.html
Copyright © 2011-2022 走看看