Python AttributeError: 'list' 对象没有属性 'copy'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37059269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:45:20  来源:igfitidea点击:

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

pythonlistnltk

提问by Amr Ragab

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.

我想在python中扩展基类列表并添加复制功能,但我不是python专家,我不知道如何解决这个问题。

采纳答案by Martijn Pieters

NLTK classifiers work with feature sets; these are always given as dictionaries with feature names mapping to a value. You are passing in a list instead, so you are not producing features as per the NLTK documentation. The code simply expects a Python dictionary, and Python dictionaries have a .copy()method.

NLTK 分类器使用特征集;这些总是作为字典给出,特征名称映射到一个值。您正在传递一个列表,因此您没有按照 NLTK 文档生成功能。代码只需要一个 Python 字典,而 Python 字典有一个.copy()方法。

See the NLTK tutorial chapter on Learning to Classify Text:

请参阅有关学习对文本进行分类NLTK 教程章节

The returned dictionary, known as a feature set, maps from feature names to their values. Feature names are case-sensitive strings that typically provide a short human-readable description of the feature, as in the example 'last_letter'. Feature values are values with simple types, such as booleans, numbers, and strings.

返回的字典,称为特征集,从特征名称映射到它们的值。功能名称是区分大小写的字符串,通常提供简短的人类可读的功能描述,如示例中所示'last_letter'。特征值是具有简单类型的值,例如布尔值、数字和字符串。

Also see the Featuresetssection of the NLTK Classify API documentation:

另请参阅NLTK Classify API 文档Featuresets部分

The features describing a token are encoded using a “featureset”, which is a dictionary that maps from “feature names” to “feature values”. Feature names are unique strings that indicate what aspect of the token is encoded by the feature.

描述令牌的特征使用“特征集”进行编码,特征集是从“特征名称”映射到“特征值”的字典。功能名称是唯一的字符串,表示该功能对令牌的哪个方面进行编码。

You haven't shared what kind of objects the train_datalist contains; if those are feature setdictionaries, you want to use classify_many()instead:

您还没有分享train_data列表包含的对象类型;如果这些是功能集词典,您想classify_many()改用:

results = classifier.classify_many(test_data)

That method doestake a list, but each element must still be a valid feature set.

该方法确实需要一个列表,但每个元素仍然必须是一个有效的功能集。

回答by Shemogumbe

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:

list.copy 方法在 python 2.x 和 python 3.x 中都不起作用,我想知道为什么它仍在文档中。要实现复制列表的结果,请使用 list 关键字:

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

Optionally, you can copy a list by slicing it:

或者,您可以通过切片来复制列表:

my_fruits_copy = fruits[:]

回答by Polipizio

Why do you want do that? if you want set a list equal to another you can do that:

你为什么要这样做?如果你想设置一个列表等于另一个你可以这样做:

a = ['a', 'b', 'c']
b = a 

else if you want insert a list inside a secondone you can use append():

否则,如果你想在第二个中插入一个列表,你可以使用 append():

a = ['a','b','c']
b = ['d','e','f']
for i in len(a):
    b.append(i-1)