美文网首页
python3 与Python2中map()函数的使用异同

python3 与Python2中map()函数的使用异同

作者: WeavingWong | 来源:发表于2019-05-06 11:31 被阅读0次

Python2 map()

python2的map(function, list)函数直接返回一个list

python3

此时返回的是迭代器,需要加一个list作用于map

list(map(frozenset,[[1],[2]]))

>> [frozenset({1}), frozenset({2})]

需要注意的是

int型数据是不能被用于迭代的,所以map的list入参的元素不能是整数,即

map(frozenset,[1,2]) 
# 这是错误的

故list中的元素类型可以是str,list等

相关文章

网友评论

      本文标题:python3 与Python2中map()函数的使用异同

      本文链接:https://www.haomeiwen.com/subject/ntlroqtx.html