2374668771 发表于 2018-4-13 21:21:46

关于extend语句的

w=[('办学', '我'), '我是', '小于', '俩张', '王羲之', 1, 1.2, 21]
>>> w.extend['我']
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
    w.extend['我']
TypeError: 'builtin_function_or_method' object is not subscriptable
>>> w.extend('我')
>>> w
[('办学', '我'), '我是', '小于', '俩张', '王羲之', 1, 1.2, 21, '我']
>>> w.extend('从','下')
Traceback (most recent call last):
File "<pyshell#72>", line 1, in <module>
    w.extend('从','下')
TypeError: extend() takes exactly one argument (2 given)
>>> w.extend[('从','下')]
Traceback (most recent call last):
File "<pyshell#73>", line 1, in <module>
    w.extend[('从','下')]
TypeError: 'builtin_function_or_method' object is not subscriptable


各位大鱼大大们,帮小鱼可好

第四时空 发表于 2018-4-13 21:30:53

w.extend(['从','下'])

2374668771 发表于 2018-4-13 21:54:56

第四时空 发表于 2018-4-13 21:30
w.extend(['从','下'])

感谢感谢,原来是括号错了

Zmynx_2017 发表于 2018-4-13 21:54:57

extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

如果只是添加单个元素,可以使用append()

append与extend的区别,就在于append()是添加元素,而extend()是用新列表拓展原列表

所以,list.extend(seq) 其中的seq应该是一个列表,所以,当你用元组的时候他会报错。

还有列表等方法后面一般都是(),不会有[],列表等索引值是[]。
页: [1]
查看完整版本: 关于extend语句的