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.
  • 相关阅读:
    sql sever 数据字典语法
    端口使用情况
    koa中间件说明
    FLIP动画思想
    跨域下载文件显示文件名
    post方法打开新页面并提交参数
    常用快捷键
    cnpm与npm安装的包不一样
    chrome devTools变量不提示,断点点击去不掉问题
    未修改的模块失效排查方法
  • 原文地址:https://www.cnblogs.com/Shinered/p/9211142.html
Copyright © 2011-2022 走看看