python - AttributeError: 'tuple' 对象没有属性 'copy'

标签 python copy attributeerror

谁能给我解释一下这个错误消息吗?

# make a copy or hand is destroyed by your test
remaining = hand.copy()

结果:

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

我可以做到这一点:

remaining = copy.copy(hand)

返回这个:

(None, {hand...})

这会导致我想要完成的任务落空,因为该函数在搜索 None 返回值时返回 False。

这是创建手的函数:

def deal_hand(n):
"""
Returns a random hand containing n lowercase letters.
At least n/3 the letters in the hand should be VOWELS.

Hands are represented as dictionaries. The keys are
letters and the values are the number of times the
particular letter is repeated in that hand.

n: int >= 0
returns: dictionary (string -> int)
"""
hand={}
num_vowels = n / 3

for i in range(num_vowels):
    x = VOWELS[random.randrange(0,len(VOWELS))]
    hand[x] = hand.get(x, 0) + 1

for i in range(num_vowels, n):    
    x = CONSONANTS[random.randrange(0,len(CONSONANTS))]
    hand[x] = hand.get(x, 0) + 1

return hand

编辑: 我一路上又换了手,并用它创建了一个元组而不是字典。谢谢!

最佳答案

错误非常明显:您有一个元组,您尝试在其上调用copy

看来您手中的对象不是您想要的对象,而是一个元组。

关于python - AttributeError: 'tuple' 对象没有属性 'copy',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38946556/

相关文章:

python - AttributeError:实例没有属性

python - 如何将 SCIKIT 中 CountVectoriser 的权重加倍以用于 TFIDF 矩阵

Python matplotlib 对数自动缩放

linux:使用rsync递归复制目录,排除所有包含特定字符串的目录

java 对象复制,数组索引越界

c++ - 如何复制一个非平凡的 C++ union ?

python - 美丽汤返回空列表

python - Python中的类实例删除

python - AttributeError: 'NoneType' 对象没有属性 'email' 。 Flask-sqlalchemy

python - GAE 属性错误