初始化列表列表 [英] initialising a list of lists
问题描述
这不是我想要它做的:
This does not what I want it to do:
a = [[]] * 6
a [3] .append(''X'')
a
[[''X''],[''X''], [''X''],[''X''],[''X''],[''X'']]
这样做我想要的:
b = [[] for _ in range(6)]
b [3] .append(''X'')
b
a = [[]] * 6
a[3].append(''X'')
a [[''X''], [''X''], [''X''], [''X''], [''X''], [''X'']]
This does what I want:
b = [[] for _ in range(6)]
b[3].append(''X'')
b
[[],[],[],[''X''],[],[]]
第一个是明确和错误的。第二个是毛茸茸的,正确。
有没有办法清楚地做到这一点?
-
Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls。 html
推荐答案
Peter Kleiweg写道:
Peter Kleiweg wrote:
这不是我想要的do:
This does not what I want it to do:
>>> a = [[]] * 6
>>> a [3] .append(''X'')
>>> a [[''X''],[''X''],[''X''],[''X''],[''X''],[''X'']]
这就是我想要的:
>>> b = [[] for _ in range(6)]
>>> b [3] .append(''X'')
>>> b
>>> a = [[]] * 6
>>> a[3].append(''X'')
>>> a [[''X''], [''X''], [''X''], [''X''], [''X''], [''X'']]
This does what I want:
>>> b = [[] for _ in range(6)]
>>> b[3].append(''X'')
>>> b
[[],[],[],[''X''],[],[]]
第一个是明确的错误。第二个是毛茸茸的,正确的。
有没有办法清楚地做到这一点?
[[], [], [], [''X''], [], []]
The first is clear and wrong. The second is hairy and right.
Is there a way to do it clear and right?
http://www.python.org/doc/faq/progra ...男性名单
< / F>
http://www.python.org/doc/faq/progra...mensional-list
</F>
Peter Kleiweg写道:
Peter Kleiweg wrote:
这不是我想要它做的:
This does not what I want it to do:
>>> a = [[]] * 6
>>> a [3] .append(''X'')
>>> a [[''X''],[''X''],[''X''],[''X''],[''X''],[''X'']]
这就是我想要的:
>>> b = [[] for _ in range(6)]
>>> b [3] .append(''X'')
>>> b
>>> a = [[]] * 6
>>> a[3].append(''X'')
>>> a [[''X''], [''X''], [''X''], [''X''], [''X''], [''X'']]
This does what I want:
>>> b = [[] for _ in range(6)]
>>> b[3].append(''X'')
>>> b
[[],[],[],[''X''],[],[]]
第一个是明确的错误。第二个是毛茸茸的,正确的。
有没有办法清楚地做到这一点?
[[], [], [], [''X''], [], []]
The first is clear and wrong. The second is hairy and right.
Is there a way to do it clear and right?
http://www.python.org/doc/faq/progra ...男性名单
< / F>
http://www.python.org/doc/faq/progra...mensional-list
</F>
Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:
Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:
Peter Kleiweg写道:
Peter Kleiweg wrote:
这不是我想要它做的:
This does not what I want it to do:
>>> a = [[]] * 6
>>> a [3] .append(''X'')
>>> a
>>> a = [[]] * 6
>>> a[3].append(''X'')
>>> a
[[''X''],[''X''],[''X''],[''X''],[''X''], [''X'']]
这就是我想要的:
[[''X''], [''X''], [''X''], [''X''], [''X''], [''X'']]
This does what I want:
>>> b = [[] for _ in range(6)]
>>> b [3] .append(''X'')
>>> b
>>> b = [[] for _ in range(6)]
>>> b[3].append(''X'')
>>> b
[[],[],[],[''X''],[],[]]
第一个是明确的和错误的。第二个是毛茸茸的,正确的。
有没有办法清楚地做到这一点?
[[], [], [], [''X''], [], []]
The first is clear and wrong. The second is hairy and right.
Is there a way to do it clear and right?
http://www.python.org/doc/faq/progra ...男性名单
换句话说:不,没有。
-
Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)S:NL,de,en,(da,ia)
info: http://www.let.rug .nl / ~kleiweg / ls.html
这篇关于初始化列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!