luofeiyu_cu 发表于 2014-04-09 17:19

如何插入元素?

word=["x1","x2","x3"]
w=["x4","x5"]
word.insert(2,w)
word
['x1', 'x2', ['x4', 'x5'], 'x3']

我需要的是:
['x1', 'x2', 'x4', 'x5', 'x3']

请问,如何实现?

timespace 发表于 2014-04-09 18:12

>>> word = ["x1","x2","x3"]
>>> w = ["x4","x5"]
>>> word = w
>>> word
['x1', 'x2', 'x4', 'x5', 'x3']

luofeiyu_cu 发表于 2014-04-17 14:49

word如何理解?

xmchenb 发表于 2014-04-17 17:11

第一个2是从第二个元素插入,插入的元素个数为2

substr函数 发表于 2014-04-17 21:25

回复 4# xmchenb

谢谢。
   

icymirror 发表于 2014-04-18 09:37

回复 3# luofeiyu_cu

xmchenb的描述是有问题的。
word是列表的slicing.
word=xxx 的意思应当是把word这个列表的开始位置(2)到结束位置(2)之间的元素,用新的元素来代替
页: [1]
查看完整版本: 如何插入元素?