• map函数


    # map函数
    # 语法:map(功能函数,可迭代对象)
    # 例如:map(lambda x:x+1,num_1)
    # 功能:把可迭代对象里面的所有元素处理一下,可迭代对象的长度不变(map不改变原来的可迭代对象)


    # 实例:
    # 1)每一个元素变为其值的平方
    num_1 = [2,4,6,8,10]
    res1 = map(lambda x:x**2,num_1)
    print(list(res1)) #map返回的是一个对象,可用list转换为列表输出
    print('----------end--------')

    # 2)每个元素自增1
    num_2 = [1,3,5,7,9]
    res2 = map(lambda x:x+1,num_2)
    print(list(res2))
    print('----------end--------')

    # 3)每个元素自减1
    num_3 = [1,2,3,4,5,6,7,8,9,10]
    res3 = map(lambda x:x-1,num_3)
    print(list(res3))
    print('----------end--------')

    # 4)将字符串小写转换为大写
    str_1 = 'information'
    res_1 = map(lambda x:x.upper(),str_1) #str.upper()将字符串大写变为小写
    print(list(res_1))
    print('----------end--------')

    # 5)将字符串大写变为小写
    str_2 = 'SIGNAL'
    res_2 = map(lambda x:x.lower(),str_2)
    print(list(res_2))
    print('-------end---------')
  • 相关阅读:
    eclipse,tortoise_svn
    sftp 命令
    shell 学习文档
    书籍,文档:shell
    V2配合proxifier以及免费ip的获取方法
    算法 | FastSLAM 1.0
    算法 | k-d树
    C++ | inline关键字和内联函数
    Python | Lambda 函数
    算法 | A*算法和权重A* 算法
  • 原文地址:https://www.cnblogs.com/shadowfolk/p/14743719.html
Copyright © 2020-2023  润新知