简体   繁体   English

[None]* 在 python 中是什么意思

[英]What does [None]* means in python

I was recently studying someone's code and a portion of code given below我最近在研究某人的代码和下面给出的部分代码

class Node:
def __init__(self, height=0, elem=None):
    self.elem = elem
    self.next = [None] * height

What does it mean by [None] * height in the above code上面代码中的[None] * height是什么意思

I know what does * operator (as multiplication and unpacking) and None means in python but this is somehow different.我知道*运算符(作为乘法和解包)和None在 python 中的含义,但这在某种程度上有所不同。

It means a list of None s with a height number of elements.这意味着一个具有height元素数的None的列表。 eg, for height = 3 , it is this list:例如,对于height = 3 ,它是这个列表:

[None, None, None]

If you do -如果你这样做 -

[element] * 3

You get -你得到 -

[element, element, element]

That's what the code does, [None] * height这就是代码的作用, [None] * height

That is, if -也就是说,如果 -

height = 4
[None] * height 
# equals [None, None, None, None]
>>> [None]  * 5
[None, None, None, None, None]

Gives you a list of size height in your case在您的情况下为您提供尺寸height列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM