map 没有len函数

错误:
TypeError:object of type ‘map’ has no len()

python 3 中的map没有len功能了
解决方案:

x = [[1, 'a'], [2, 'b'], [3, 'c']]
len(map(lambda a: a[0], x))
  1. 讲map强制转换成list 或者 tuple , 然后再求len
len(list(map(lambda a: a[0], x)))
  1. 使用列表推导,不使用map
my_list = [a[0] for a in x]
len(my_list)

猜你喜欢

转载自blog.csdn.net/qq_36321330/article/details/106910773