zoukankan      html  css  js  c++  java
  • python lambda用法

    1.map()用法:

    people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
    
    def split_title_and_name(person):
        person_split = person.split()
        return person_split[0]+person_split[2]
    
    list(map(split_title_and_name, people))
    
    """ output:
    ['Dr.Brooks', 'Dr.Collins-Thompson', 'Dr.Vinod', 'Dr.Romero']
    """

    2.关于lambda函数的用法

    people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
    
    def split_title_and_name(person):
        return person.split()[0] + ' ' + person.split()[-1]
    
    #option 1
    for person in people:
        print(split_title_and_name(person) == (lambda x: x.split()[0] + ' ' + x.split()[-1])(person))
    
    #option 2
    list(map(split_title_and_name, people)) == list(map(lambda person: person.split()[0] + ' ' + person.split()[-1], people))

    3.关于list的用法

    def times_tables():
        lst = []
        for i in range(10):
            for j in range (10):
                lst.append(i*j)
        return lst
    
    times_tables() == [j*i for i in range(10) for j in range(10)]
    lowercase = 'abcdefghijklmnopqrstuvwxyz'
    digits = '0123456789'
    
    correct_answer = [a+b+c+d for a in lowercase for b in lowercase for c in digits for d in digits]
    
    correct_answer[:50] 
    The Safest Way to Get what you Want is to Try and Deserve What you Want.
  • 相关阅读:
    Django Rest framework 之 节流
    Django Rest framework 之 权限
    Django Rest framework 之 认证
    url参数和字典的相互转化
    Ajax之跨域请求
    爬虫之爬取B站关键字
    Django之ModelForm组件
    C语言逻辑运算符顺序
    2.4.4 N-S流程图表示法
    2.4.3 三种基本结构和改进的流程图
  • 原文地址:https://www.cnblogs.com/Shinered/p/9211142.html
Copyright © 2011-2022 走看看