Typeerror: Object Of Type 'map' Has No Len() Python3
I'm trying to implement KMeans algorithm using Pyspark it gives me the above error in the last line of the while loop. it works fine outside the loop but after I created the loop i
Solution 1:
You are getting this error because you are trying to get len
of map
object (of generator type) which do not supports len
. For example:
>>> x = [[1, 'a'], [2, 'b'], [3, 'c']]
# `map` returns object of map type>>> map(lambda a: a[0], x)
<mapobject at 0x101b75ba8>
# on doing `len`, raises error>>> len(map(lambda a: a[0], x))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type'map' has no len()
In order to find the length, you will have to type-cast the map
to list
(or tuple
) and then you may call len
over it. For example:
>>>len(list(map(lambda a: a[0], x)))
3
Or it is even better to simply create a list using the list comprehension (without using map
) as:
>>> my_list = [a[0] for a in x]
# since it is a `list`, you can take it's length>>> len(my_list)
3
Post a Comment for "Typeerror: Object Of Type 'map' Has No Len() Python3"