Object of type 'map' has no len() in Python 3

前端 未结 2 1519
耶瑟儿~
耶瑟儿~ 2020-12-03 13:12

I have a problem with Python 3. I got Python 2.7 code and at the moment I am trying to update it. I get the error:

TypeError: object of type \'map\' h

2条回答
  •  感情败类
    2020-12-03 13:58

    In Python 3, map returns a map object not a list:

    >>> L = map(str, range(10))
    >>> print(L)
    
    >>> print(len(L))
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: object of type 'map' has no len()
    

    You can convert it into a list then get the length from there:

    >>> print(len(list(L)))
    10
    

提交回复
热议问题