插入列表 [英] inserting into a list
问题描述
请允许我事先道歉,因为我确定这是一个非常简单的问题,但我无法在我的任何一本Python书籍中找到答案。
我已经尝试了一些交互式提示测试,但他们也没有工作
。
所有我''我试图做的是将一个项目插入一个列表,如下:
L = [1,2,4]
和我想要将整数3插入位置L [2],这样
列表会显示[1,2,3,4]
我'我已经尝试了各种切片分配的组合,但我总是
得到:
TypeError:只能分配一个可迭代的
>
有人可以用简单的答案让我难堪吗? :)
Let me apologize in advance for what I''m sure is an achingly simple
question, but I just can''t find the answer in either of my Python books.
I''ve tried a few tests with the interactive prompt, but they don''t work
either.
All I''m trying to do is insert an item into a list, like so:
L = [1, 2, 4]
and I want to insert the integer 3 into the position L[2], so that the
list reads [1, 2, 3, 4]
I''ve tried all kinds of combinations of slicing assignment, but I always
get:
TypeError: can only assign an iterable
Can someone please embarrass me with the simple answer? :)
推荐答案
2006年3月7日星期二16:18,John Salerno写道:
On Tuesday 07 March 2006 16:18, John Salerno wrote:
让我提前道歉我肯定是一个非常简单的问题,但我在我的任何一本Python书中找不到答案。
我已经尝试了一些交互式提示测试,但他们也不工作。
所有我想做的就是将一个项目插入一个列表,如下:
L = [1,2,4]
我想将整数3插入位置L [2],以便
列表读取[1,2,3,4]
Let me apologize in advance for what I''m sure is an achingly simple
question, but I just can''t find the answer in either of my Python books.
I''ve tried a few tests with the interactive prompt, but they don''t work
either.
All I''m trying to do is insert an item into a list, like so:
L = [1, 2, 4]
and I want to insert the integer 3 into the position L[2], so that the
list reads [1, 2, 3, 4]
要么
L [2:2] = [3]
或者
L.insert(2,3)
亲切的
Christoph
-
~
~
" .signature" [修改] 1行--100% - 1,48全部
Either
L[2:2]=[3]
or
L.insert(2,3)
Kindly
Christoph
--
~
~
".signature" [Modified] 1 line --100%-- 1,48 All
John Salerno写道:
John Salerno wrote:
让我道歉我确信这是一个非常简单的问题,但是我无法在我的任何一本Python书籍中找到答案。
我已经尝试了一些交互式测试提示,但他们也不工作。
所有我想做的就是将一个项目插入一个列表,如下:
L = [1,2,4]
我想将整数3插入位置L [2],以便
列表读取[1,2,3,4] ]
我已经尝试了各种切片分配组合,但我总是得到:
TypeError:只能指定一个可迭代的
有人可以用简单的答案让我难堪吗? :)
Let me apologize in advance for what I''m sure is an achingly simple
question, but I just can''t find the answer in either of my Python books.
I''ve tried a few tests with the interactive prompt, but they don''t work
either.
All I''m trying to do is insert an item into a list, like so:
L = [1, 2, 4]
and I want to insert the integer 3 into the position L[2], so that the
list reads [1, 2, 3, 4]
I''ve tried all kinds of combinations of slicing assignment, but I always
get:
TypeError: can only assign an iterable
Can someone please embarrass me with the simple answer? :)
l = [1,2,3]
l.insert (2,10)
l
[1,2,10,3]
l = [1,2,3]
l.insert(2, 10)
l [1, 2, 10, 3]
足够尴尬?
Diez
Embarrasing enough?
Diez
Diez B. Roggisch写道:
Diez B. Roggisch wrote:
l = [1,2,3]
l.insert(2,10)
l
l = [1,2,3]
l.insert(2, 10)
l
[1,2,10,3]
足够尴尬吗?
[1, 2, 10, 3]
Embarrasing enough?
实际上,我试图用切片技术搞清楚br />
而不是。但是,正如克里斯托弗的例子所示,这并不难。但是我没有意识到你必须给切片分配一个列表项,所以我在做:
L [2:2] = 3
等等,但它们都涉及''= 3'',而不是''= [3]''
Actually, I was trying to figure it out with the slice technique
instead. But yeah, as Christopher''s example showed, it''s not hard. But I
didn''t realize you had to assign a list item to the slice, so I was doing:
L[2:2] = 3
among other things, but they all involved ''= 3'', not ''= [3]''
这篇关于插入列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!