Discuss / Python / 关于tuple与list结合的小问题

关于tuple与list结合的小问题

Topic source

Crazy_tim925

#1 Created at ... [Delete] [Delete and Lock User]

前面讲到在tuple中可以套用list,例如:

language('JAVA','C#',['asp','php'],‘C++')

在language[2]这个元素中能否使用insert、append呢?

我自己试了几种写法,都有报错

>>> language[2].append='aspx'   
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'list' object attribute 'append' is read-only
>>> language[2][0].append='aspx'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'append'

如果可以使用insert、append的话,格式应该怎么写呢?

纯小白,忘勿喷,感激不尽!

Kenny_SSS

#2 Created at ... [Delete] [Delete and Lock User]

append和insert都是方法,如果你要在language[2]这个list里使用append这个方法来添加‘aspx’,可以这么写:

language[2].append('aspx')

  • 1

Reply