CountVectorizer on list of integers

Viewed 1169

I have list of integers as below:

mylist = [111,113,114,115,112,115,234,643,565,.....]

I have many lists like this with more than 500 integers on which I wanted to run CountVectorizer. As far as I know, CountVectorizer only tokenize list of string than integers.

I have tried to first convert each element of these lists into str via

mylist_string = list(map(lambda x: str(x), mylist)) but since the list is too long, it is taking very large time.

Is there any way to tokenize the integer lists or is there any efficient way to convert the list of numbers to list of strings.

Thank you

1 Answers

For your case, its redundant to use map with lambda, that might be the reason for the slow down, you could just use map without lambda like below

mylist = [111,113,114,115,112,115,234,643,565]
mylist_string = map(str, mylist) # use list(map(str, mylist)) for python 3
# ['111', '113', '114', '115', '112', '115', '234', '643', '565']

alternatively, you could try list comprehension

mylist = [111,113,114,115,112,115,234,643,565]
mylist_string = [str(x) for x in mylist]
# ['111', '113', '114', '115', '112', '115', '234', '643', '565']
Related