如何在一行上声明多个数组变量-python

时间:2017-06-14 02:09:07

标签: python syntax variable-declaration

为了不使用这么多行来声明这样的变量:

open = []

high = []

low = []

close = []

我可以这样做:

open,high,low,close = []

实际上有效

1 个答案:

答案 0 :(得分:1)

没有。你问题中的例子不起作用。它会引发ValueError。这是因为Python试图将四个值解压缩到openhighlowclosed,但是如果找不到任何值(这会在Python中引发错误) 2.x和Python 3.x):

>>> open, high, low, close = []
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 4, got 0)
>>> 

要初始化同一行上的所有列表变量,您需要提供所有四个变量的列表:

>>> open, high, low, closed = [], [], [], []
>>> 
>>> open
[]
>>> high
[]
>>> low
[]
>>> closed
[]
>>>