zoukankan      html  css  js  c++  java
  • 8map

    """
    map(func,iterable)
    功能:对传入的可迭代数据进行处理,返回一个迭代器
    
    参数:
        func函数  自定义函数|内置函数
        iterables:可迭代的数据
    返回值: 迭代器
    
    """
    

    1把一个字符串数字列表,转为整型的数字列表

    • 普通的方法
    
    varlist = ['1','2','3','4'] # 变成==>[1,2,3,4]
    
    newlist = []
    for i in varlist:
        newlist.append(int(i))
    print(newlist)
    
    
    E:\python3810\python.exe D:/py/test/gao/函数-map.py
    [1, 2, 3, 4]
    
    • 用map函数实现
    varlist = ['1','2','3','4'] # 变成==>[1,2,3,4]
    res = map(int,varlist)      #把 varlist里的元素,依次拿出来,给int函数进行转换整数操作,返回res迭代器 #<map object at 0x0000025E54557A00>
    print(list(res))            #用list函数执行 res迭代器
    
    E:\python3810\python.exe D:/py/test/gao/函数-map.py
    [1, 2, 3, 4]
    

    2有一个列表 [1,2,3,4] 转成 [25,4,9,16]

    • 普通方法实现:
    list01 = [5,2,3,4]
    newlist02 = []
    for i in list01:
        n = i ** 2
        newlist02.append(n)
    print(newlist02)
    
    • map传入自定义参数实现
    list01 = [5,2,3,4]
    def func(n):
        return n ** 2
    res = map(func,list01) #res是 #<map object at 0x0000025E54557A00>
    print(res,list(res))
    
    [25, 4, 9, 16]
    

    3把['a','b','c','d'] #转成==>[66,66,67,68]

    ls = ['a','b','c','d']
    ls = map(lambda n: ord(n),ls)      #这句话从后向前看, ls列表中的每个数据依次拿出来,给ord函数里面的参数 n处理,
    print(list(ls))
    
    E:\python3810\python.exe D:/py/test/gao/函数-map.py
    [97, 98, 99, 100]
    
    
    ls = ['a','b','c','d']
    print(list(map ord,ls)))
    
  • 相关阅读:
    Java消息队列-Spring整合ActiveMq
    控制 Memory 和 CPU 资源的使用
    真的了解js生成随机数吗
    vue原来可以这样上手
    Weex系列一、构建Weex工程
    MS Word 目录排版
    mac上使用终端编译omp代码
    x的平方根
    如何进行特征选择
    单词模式
  • 原文地址:https://www.cnblogs.com/john5yang/p/15631248.html
Copyright © 2011-2022 走看看