Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'list' object has no attribute 'copy'

Tags:

python

list

nltk

I have the following code snippet

classifier = NaiveBayesClassifier.train(train_data)
#classifier.show_most_informative_features(n=20)
results = classifier.classify(test_data)

and the error shows in the following line

results = classifier.classify(test_data)

error:

Traceback (most recent call last):
  File "trial_trial.py", line 46, in <module>
    results = classifier.classify(test_data)
  File "c:\Users\Amr\Anaconda\lib\site-packages\nltk\classify\naivebayes.py", line 88, in classify
    return self.prob_classify(featureset).max()
  File "c:\Users\Amr\Anaconda\lib\site-packages\nltk\classify\naivebayes.py", line 94, in prob_classify
    featureset = featureset.copy()
AttributeError: 'list' object has no attribute 'copy'

I think of extending base class list in python and add copy function but I'm not expert in python and I don't know how to solve this problem.

like image 877
Amr Ragab Avatar asked May 05 '16 20:05

Amr Ragab


People also ask

How do you fix an object that has no attribute?

If you are getting an object that has no attribute error then the reason behind it is because your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.


1 Answers

The list.copy method does not work both in python 2.x and python 3.x, I wonder why it is still in the documentation. To achieve the results of copying a list, user the list keyword:

fruits = ['banana', 'cucumber', 'apple', 'water mellon']
my_fruits = list(fruits)

Optionally, you can copy a list by slicing it:

my_fruits_copy = fruits[:]
like image 113
Shemogumbe Avatar answered Sep 24 '22 18:09

Shemogumbe