创建空列表列表 [英] Creating a List of Empty Lists
问题描述
Pythons内部''指针''系统肯定会给我带来一些
头疼.....当我想复制一个变量的内容时我发现
我不可能知道我是否复制了内容*或*只是
创建了一个指向原始值的新指针....
For例子我想初始化一个空列表列表....
a = [[],[],[],[],[]]
>
我认为必须有一个*非常简单的方法 - 在一次黑客攻击之后我发现:
a = [[] ] * 10似乎工作
然而 - 在我的程序中使用它称为奇怪的崩溃....
我最终发现(作为一个愚蠢的例子) :
a = [[]] * 10
b = -1
而b < 10:
b + = 1
a [b] = b
打印一份
制作:
[[9],[9],[9] ......
这根本不是我想要的...... .......
这样做的正确,快捷的方法是什么(不使用循环和
追加......) ?
Fuzzyman
http://www.Voidspace.org.uk
顶空与网络空间相遇的地方。在线资源网站 -
涵盖科学,技术,计算机,计算机朋克,心理学,
灵性,小说等等。
- -
http://www.atlantibots.org.uk
http://groups.yahoo.com/group/atlantis_talk/
Atlantibots - 踩踏亚特兰蒂斯世界。
---
http://www.fuchsiashockz.co.uk
http://groups.yahoo.com/group/void-shockz
---
>
每个人都有才华。什么是罕见的是勇气跟随人才
到它所带领的黑暗地方。 -Erica Jong
野心是一个糟糕的借口,因为没有足够的感觉可以懒惰。
-Milan Kundera
mi*****@foord.net (Fuzzyman)写在
news:80 ************************** @ posting.google.c om:
我最终发现(作为一个愚蠢的例子):
a = [[]] * 10
b = -1
而b< 10:
b + = 1
a [b] = b
打印一份
制作:
[[9],[9],[ 9] ......
这根本不是我想要的...............
什么是正确的,快速做到这一点(不使用循环和
附加...)?
这些天的推荐方式通常是:
a = [[] for i in range(10)]
它仍然有一个循环并通过附加空列表来工作,但至少它的
只是一个表达式。此外,您可以很容易地将
初始化的下一阶段纳入其中:
a = [[b]对于范围内的b(10)]
-
Duncan Booth du****@rcp.co。英国
int month(char * p){return(124864 /((p [0] + p [1] -p [2]& 0x1f)+1)%12 )[" \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 4"];} //谁说我的代码模糊不清?
Fuzzyman写道:Pythons内部''指针''系统肯定是引起我几个头疼.....当我想复制一个变量的内容时我发现
我不可能知道我是否复制了内容*或*只是
创建了一个指向原始值的新指针....
例如,我想初始化一个空列表列表....
a = [[],[ ],[],[],[]]
[。 ..]正确,快速的方法是什么(不使用循环和
附加......)?
< blockquote class =post_quotes>l = [[] for x in xrange(3)]
l
[[],[],[]] l [0 ] .append('''')
l
[[''''],[],[]]
Daniel
mi ***** @ foord.net (Fuzzyman)写道:
蟒蛇内部''指针''系统肯定会让我产生一些
头疼.....当我想复制变量的内容时,我发现
无法知道我是否已复制内容*或*只是
创建了一个指向原始值的新指针....
例如我想初始化一个空列表列表....
a = [[],[],[],[], []]
我认为必须有一个*真的*简单的方法 - 在一次黑客攻击之后我发现:
a = [[]] * 10似乎工作了
然而 - 在我的程序中使用它叫做奇怪的崩溃....
我最终发现(作为一个愚蠢的例子):
a = [[]] * 10
b = -1
而b< 10:
b + = 1
a [b] = b
打印一份
制作:
[[9],[9],[ 9] ......
这根本不是我想要的...............
什么是正确的,这样做的快捷方式(不使用循环和
附加......)?
这里产生了一个IndexError。在改变b< 10 QUOT;进入b< 9
代码制作:
[0,1,2,3,4,5,6,7,8,9] >
我看到其他一些海报已经给你答案了。我会做一些不同的事情,给你一个问题:-)
n = 4
agent = [[] ] * n
打印代理商
代理商[0] .append(''史密斯'')
打印代理商
neos = map(list,[[]] * n)
print neos
neos [0] .append(''Neo'')
打印neos
输出为:
[[],[],[],[]]
[[''史密斯''],[''史密斯'',[''史密斯''],[''史密斯'']
[[],[ ],[],[]]
[[''Neo''],[],[],[]]
问题是:
为什么史密斯?复制到矩阵中的所有元素?
(或者是另一部电影:-)
Anton
Pythons internal ''pointers'' system is certainly causing me a few
headaches..... When I want to copy the contents of a variable I find
it impossible to know whether I''ve copied the contents *or* just
created a new pointer to the original value....
For example I wanted to initialize a list of empty lists....
a=[ [], [], [], [], [] ]
I thought there has to be a *really* easy way of doing it - after a
bit of hacking I discovered that :
a = [[]]*10 appeared to work
however - using it in my program called bizarre crashes....
I eventually discovered that (as a silly example) :
a = [[]]*10
b=-1
while b < 10:
b += 1
a[b] = b
print a
Produced :
[ [9], [9], [9]......
Which isn''t at all what I intended...............
What is the correct, quick way of doing this (without using a loop and
appending...) ?
Fuzzyman
http://www.Voidspace.org.uk
The Place where headspace meets cyberspace. Online resource site -
covering science, technology, computing, cyberpunk, psychology,
spirituality, fiction and more.
---
http://www.atlantibots.org.uk
http://groups.yahoo.com/group/atlantis_talk/
Atlantibots - stomping across the worlds of Atlantis.
---
http://www.fuchsiashockz.co.uk
http://groups.yahoo.com/group/void-shockz
---
Everyone has talent. What is rare is the courage to follow talent
to the dark place where it leads. -Erica Jong
Ambition is a poor excuse for not having sense enough to be lazy.
-Milan Kundera
mi*****@foord.net (Fuzzyman) wrote in
news:80**************************@posting.google.c om:
I eventually discovered that (as a silly example) :
a = [[]]*10
b=-1
while b < 10:
b += 1
a[b] = b
print a
Produced :
[ [9], [9], [9]......
Which isn''t at all what I intended...............
What is the correct, quick way of doing this (without using a loop and
appending...) ?
The recommended way these days is usually:
a = [ [] for i in range(10) ]
That still has a loop and works by appending empty lists, but at least its
just a single expression. Also you can incorporate the next stage of your
initialisation quite easily:
a = [ [b] for b in range(10) ]
--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Fuzzyman wrote:Pythons internal ''pointers'' system is certainly causing me a few
headaches..... When I want to copy the contents of a variable I find
it impossible to know whether I''ve copied the contents *or* just
created a new pointer to the original value....
For example I wanted to initialize a list of empty lists....
a=[ [], [], [], [], [] ] [...] What is the correct, quick way of doing this (without using a loop and
appending...) ?
l = [ [] for i in xrange (3)]
l [[], [], []] l [0].append (''a'')
l
[[''a''], [], []]
Daniel
mi*****@foord.net (Fuzzyman) wrote:
Pythons internal ''pointers'' system is certainly causing me a few
headaches..... When I want to copy the contents of a variable I find
it impossible to know whether I''ve copied the contents *or* just
created a new pointer to the original value....
For example I wanted to initialize a list of empty lists....
a=[ [], [], [], [], [] ]
I thought there has to be a *really* easy way of doing it - after a
bit of hacking I discovered that :
a = [[]]*10 appeared to work
however - using it in my program called bizarre crashes....
I eventually discovered that (as a silly example) :
a = [[]]*10
b=-1
while b < 10:
b += 1
a[b] = b
print a
Produced :
[ [9], [9], [9]......
Which isn''t at all what I intended...............
What is the correct, quick way of doing this (without using a loop and
appending...) ?
Here it produced an IndexError. After changing "b < 10" into "b < 9"
the code produced:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
I see some other posters already gave you the answer. I''ll do
something different and give you the question :-)
n = 4
agents = [[]]*n
print agents
agents[0].append(''Smith'')
print agents
neos = map(list,[[]]*n)
print neos
neos[0].append(''Neo'')
print neos
output is:
[[], [], [], []]
[[''Smith''], [''Smith''], [''Smith''], [''Smith'']]
[[], [], [], []]
[[''Neo''], [], [], []]
The question is:
Why is "Smith" copied to all elements in the matrix?
(or is that another movie :-)
Anton
这篇关于创建空列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!