'FreqDist on a list giving error 'unhashable type: list'
I thought FreqDist took in a list and then have you back the counts? I have a list called ngrams
ngrams = make_ngram_tuples(tokenized_sents, n)
print(type(ngrams))
dist = FreqDist(ngrams)
print(type(ngrams))
<class 'list'>```
But I also get the error TypeError: unhashable type: 'list'. Any clue what could be going on?
Solution 1:[1]
If ngrams is a list of lists, as you've indicated in a comment to your question, then FreqDist() may be attempting to create a dictionary using the elements of ngrams as keys. This will be a problem, as the element datatype list is not hashable in Python. A tuple would be hashable, so you could try the following updated code to fix it by converting each list element within the ngrams list to a tuple before passing ngrams as an argument to FreqDist():
ngrams = make_ngram_tuples(tokenized_sents, n)
ngrams = [tuple(v for v in ngrams)]
dist = FreqDist(ngrams)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | constantstranger |
