定义使用乘法运算符在Python列表 [英] Defining a list in Python using the multiply operator
问题描述
近日在Python我也遇到过这样一句话:
board.append([''] * 8)
我试图搜索与谷歌在互联网上找到这种类型的声明更多的信息,但我做不到。
我知道的什么的声明的做法,但我不明白的如何的,用什么方式做,这一点。
这是我第一次看到了 *
清单上使用运营商。你可以请参考我一个地方,我可以找到这种类型的报表的一些更多的信息?
您可以请参考我一个地方,我可以找到这种类型的报表的更多信息。
块引用>大多数相关运营商和方法都在这里定义的:<一href=\"http://docs.python.org/2/library/stdtypes.html#sequence-types-str-uni$c$c-list-tuple-bytearray-buffer-xrange\"相对=nofollow>序类型的。
具体
S *ñ
定义为
S * N,* S
-N
的浅拷贝小号
串联
块引用>在这里,
取值
是一个序列和N
是一个数字。因此,
[''] * 8
返回由8个名单。
board.append()
追加结果板
,其中presumably的名单(共列表)。Recently in Python I have encountered this statement:
board.append([' '] * 8)
I have tried to search the Internet with Google to find some more information about this type of statement, but I can't.
I know what the statement does, but I do not understand how, in what manner is doing, that.
This is the first time I have seen the
*
operator used on a list. Can you please refer me to a place where I can find some more information about this type of statements?解决方案Can you please refer me to a place where I can find some more information about this type of statements.
Most of the relevant operators and methods are defined here: Sequence Types.
Specifically
s * n
is defined as
s * n, n * s
--n
shallow copies ofs
concatenatedHere,
s
is a sequence andn
is a number.Thus,
[' '] * 8
returns a list consisting of eight' '
.
board.append()
appends the result toboard
, which presumably is a list (of lists).这篇关于定义使用乘法运算符在Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!