Python AttributeError: 'str' 对象没有属性 'append'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4005796/
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
AttributeError: 'str' object has no attribute 'append'
提问by Zeynel
>>> myList[1]
'from form'
>>> myList[1].append(s)
Traceback (most recent call last):
File "<pyshell#144>", line 1, in <module>
myList[1].append(s)
AttributeError: 'str' object has no attribute 'append'
>>>
Why myList[1]is considered a 'str'object? mList[1]returns the first item in the list 'from form'but I cannot append to item 1 in the list myList. Thank you.
为什么myList[1]被认为是'str'对象?mList[1]返回列表中的第一项,'from form'但我无法附加到列表中的第 1 项myList。谢谢你。
Edit01:
编辑01:
@pyfunc: Thank you for the explanation; now I understand.
@pyfunc:谢谢你的解释;现在我明白了。
I need to have a list of lists; so 'from form' should be a list. I did this (please correct if this not the right way):
我需要一份清单;所以“来自表单”应该是一个列表。我这样做了(如果这不是正确的方式,请更正):
>>> myList
[1, 'from form', [1, 2, 't']]
>>> s = myList[1]
>>> s
'from form'
>>> s = [myList[1]]
>>> s
['from form']
>>> myList[1] = s
>>> myList
[1, ['from form'], [1, 2, 't']]
>>>
采纳答案by pyfunc
myList[1] is an element of myList and it's type is string.
myList[1] 是 myList 的一个元素,它的类型是字符串。
myList[1] is str, you can not append to it. myList is a list, you should have been appending to it.
myList[1] 是 str,您不能附加到它。myList 是一个列表,您应该一直附加到它。
>>> myList = [1, 'from form', [1,2]]
>>> myList[1]
'from form'
>>> myList[2]
[1, 2]
>>> myList[2].append('t')
>>> myList
[1, 'from form', [1, 2, 't']]
>>> myList[1].append('t')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'
>>>
回答by bstpierre
If you want to append a value to myList, use myList.append(s).
如果要将值附加到 myList,请使用myList.append(s).
Strings are immutable -- you can't append to them.
字符串是不可变的——你不能附加到它们。
回答by bstpierre
Why myList[1] is considered a 'str' object?
为什么 myList[1] 被认为是一个“str”对象?
Because it is a string. What else is 'from form', if not a string? (Actually, strings are sequences too, i.e. they can be indexed, sliced, iterated, etc. as well - but that's part of the strclass and doesn't make it a list or something).
因为它是一个字符串。还有什么'from form',如果不是一个字符串?(实际上,字符串也是序列,即它们也可以被索引、切片、迭代等 - 但这是str类的一部分,并没有使它成为列表或其他东西)。
mList[1]returns the first item in the list'from form'
mList[1]返回列表中的第一项'from form'
If you mean that myListis 'from form', no it's not!!! The second(indexing starts at 0) elementis 'from form'. That's a BIG difference. It's the difference between a house and a person.
如果你的意思myList是'from form',不,它不是!!!所述第二(索引开始于0)元素是'from form'。这是一个很大的不同。这就是房子和人的区别。
Also, myListdoesn't have to be a listfrom your short code sample - it could be anything that accepts 1as index - a dict with 1 as index, a list, a tuple, most other sequences, etc. But that's irrelevant.
此外,myList不必是list您的短代码示例中的一个 - 它可以是任何接受1作为索引的东西- 一个以 1 作为索引的字典、一个列表、一个元组、大多数其他序列等。但这无关紧要。
but I cannot append to item 1 in the list
myList
但我无法附加到列表中的第 1 项
myList
Of course not, because it's a string and you can't append to string. String are immutable. You can concatenate (as in, "there's a new object that consists of these two") strings. But you cannot append(as in, "this specific object now has this at the end") to them.
当然不是,因为它是一个字符串,你不能附加到字符串。字符串是不可变的。您可以连接(如“有一个由这两个组成的新对象”)字符串。但是你不能append(比如,“这个特定的对象现在最后有这个”)。
回答by Dr. Bob
What you are trying to do is add additional information to each item in the list that you already created so
您要做的是向已创建的列表中的每个项目添加附加信息
alist[ 'from form', 'stuff 2', 'stuff 3']
for j in range( 0,len[alist]):
temp= []
temp.append(alist[j]) # alist[0] is 'from form'
temp.append('t') # slot for first piece of data 't'
temp.append('-') # slot for second piece of data
blist.append(temp) # will be alist with 2 additional fields for extra stuff assocated with each item in alist
回答by Reshma
This is simple program showing append('t') to the list.
n=['f','g','h','i','k']
这是显示 append('t') 到列表的简单程序。
n=['f','g','h','i','k']
for i in range(1):
temp=[]
temp.append(n[-2:])
temp.append('t')
print(temp)
Output: [['i', 'k'], 't']
输出:[['i', 'k'], 't']

