-2

I am executing following lines of code. Vector file is produced using word2vec.

I am getting an error on this line:

vector = map(float, fields)
length_of_vectors = len(vector)

Error:

TypeError: object of type 'map' has no len()

How can i resolve this error?

I have tried converting

vector = map(float, fields) 

to

vector = list(map(float, fields))

but it gives following error: ValueError: could not convert string to float

with io.open(vec_file, 'r', encoding="ISO-8859-1") as f:
    for line in f:
        line = line.strip()
        if line == '':
            continue
        word = line[0:line.index(' ')]
        rest = line[line.index(' ') + 1:]
        fields = rest.split(' ')
        vector = map(float, fields)
        length_of_vectors = len(vector)
        word_vector_dictionary[word] = vector
f.close()
  • 2
    Possible duplicate of [Object of type 'map' has no len() in Python 3](https://stackoverflow.com/questions/36982858/object-of-type-map-has-no-len-in-python-3) – Sayse Jul 02 '19 at 09:20
  • What is `fields` containing? – Lê Tư Thành Jul 02 '19 at 09:21
  • 2
    @Sayse If you read, they already tried that. Their actual issue is they are trying to convert strings to floats. Probably worth closing for a different reason though – Chris_Rands Jul 02 '19 at 09:21
  • 1
    @Chris_Rands - I did read, they also state near the top "How can I resolve this error?" as well as it being the questions title, the rest of the question should be a seperate question which is no doubt also a duplicate as the file probably contains data that isn't a float – Sayse Jul 02 '19 at 09:23

1 Answers1

0

this gives error because you give map obj to len(), If you looking for length of each tuple then try this one

def lenfun(i):
    return len(i)

length_of_vectors = map(lenfun,list_of_tuple)
print(length_of_vectors)

error convert string to float :

in a given code 'fields' is a string,but in line -> vector = map(float, fields) you convert string 'fields' to float 'fields'(containing numbers,letters),letters CANNOT be converted into float