0
点赞
收藏
分享

微信扫一扫

Python3中利用map将列表当中的string类型转换为int类型或其它类型


看下面的代码

list1 = ['11','22']
list1 = map(int, list1)
print(type(list1[0]))

编译后会报错​​TypeError: 'map' object is not subscriptable​​​,在百度了后才知道,上面代码在python2当中才是正确的,在py3中,它返回的是迭代器,不是我们直接想要的list。
所以正确的做法如下:

list1 = ['11','22']
list1 = list(map(int, list1))
print(type(list1[0]))


举报

相关推荐

0 条评论