python - Count how often a specific string occurs in a list -
i want pairwise compare several lists in kind of "bag of words" approach. have strings in lists.
unfortunatelly, have bug in script cannot fix.
code works if there numbers in lists have strings in lists doesn't run anymore. appreciate help.
i receive following error message:
traceback (most recent call last): file "test.py", line 21, in <module> bow_matrix[0, p] = list_words_ab[p] valueerror: not convert string float: 'd'
my code:
a = ["a", "b", "c", "d"] b = ["b", "c", "d", "e"] p = 0 if len(a) > len(b): max_words = len(a) else: max_words = len(b) list_words_ab = list(set(a) | set(b)) len_bow_matrix = len(list_words_ab) bow_matrix = numpy.zeros(shape = (3, len_bow_matrix)) while p < len_bow_matrix: bow_matrix[0, p] = list_words_ab[p] p = p+1 p = 0 while p < len_bow_matrix: bow_matrix[1, p] = a.count(bow_matrix[0, p]) bow_matrix[2, p] = b.count(bow_matrix[0, p]) p = p+1
by default numpy.zeros
makes empty array of floats, use strings need specify dtype=str
:
bow_matrix = numpy.zeros(shape = (3, len_bow_matrix),dtype=str)
Comments
Post a Comment