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)))
posted @ 2021-12-01 23:10  john5的博客  阅读(74)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css