TypeError: 'tuple' object does not support item assignment

在python3中list和tuple都是有序的,二者最大的区别就是tuple中的元素不可更改:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a_tuple = ('abc', 1, 2, 3)
>>> a_list  = ['abc', 1, 2, 3]
>>> print(type(a_tuple), type(a_list), sep=' - ')
<class 'tuple'> - <class 'list'>
>>> a_list[1] += 10
>>> a_list
['abc', 11, 2, 3]
>>> a_tuple[1] += 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> 

猜你喜欢

转载自blog.csdn.net/AnonymKing/article/details/81141504