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.
  • 相关阅读:
    js对象写法
    IE6双边距bug及其解决办法
    图片轮播
    盒子水平和垂直同时居中方法
    选项卡切换
    针对IE6兼容png
    html5兼容
    sublime快捷键总结
    七种设计原则
    Git基本命令
  • 原文地址:https://www.cnblogs.com/Shinered/p/9211142.html
Copyright © 2011-2022 走看看