def get_word_count(wordlist, final):
regex = []
count = [[] for x in xrange(len(wordlist))]
frequency = []
regex = makeregex(wordlist)
for i in range(len(final)-1):
size = os.stat(final[i]).st_size
fil = open(final[i])
if(fil):
print final[i] + " read!"
data = mmap.mmap(fil.fileno(), size, access=mmap.ACCESS_READ)
for j in range (len(wordlist)):
count[j].append(re.findall(regex[j], data))
fil.close()
for k in range(len(wordlist)):
frequency.append(sum(count[k]))
print frequency
count is a list of lists and every list has some numbers stored into it. I wish to store the sum of every list as an element to a new list frequency
When I run the code I get an error :
Traceback (most recent call last):
File "C:\Users\Animesh\Desktop\_zipf.py", line 52, in <module>
get_word_count(wordlist, final)
File "C:\Users\Animesh\Desktop\_zipf.py", line 32, in get_word_count
frequency.append(sum(count[k]))
TypeError: unsupported operand type(s) for +: 'int' and 'list'
What should I change in my code ?
Please help
You’re adding list of found words by the regex to the array
count[j], so eachcountelement is a list of list of strings, thus the error when callingsum(count[k]).I think you want to append to
count[k]the number of found words: