定义使用乘法运算符在Python列表 [英] Defining a list in Python using the multiply operator

查看:110
本文介绍了定义使用乘法运算符在Python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

近日在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 of s concatenated

Here, s is a sequence and n is a number.

Thus, [' '] * 8 returns a list consisting of eight ' '.

board.append() appends the result to board, which presumably is a list (of lists).

这篇关于定义使用乘法运算符在Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆