Is there a way to init a list without using square bracket in Python?

Is there a way to init a list without using square bracket in Python?

For example, is there a function like list_cons so that:

x = list_cons(1, 2, 3, 4)

is equivalent to:

x = [1, 2, 3, 4]

In [1]: def list_cons(*args):
   ...:     return list(args)
   ...: 

In [2]: list_cons(1,2,3,4)
Out[2]: [1, 2, 3, 4]

使用列表构造函数并将它传递给一个元组。

x = list((1,2,3,4))

I don't think that would be a particularly useful function. Is typing brackets so hard? Perhaps we could give you a more useful answer if you explained why you want this.

Still, here's a fun thing you can do in Python 3:

>>> (*x,) = 1, 2, 3, 4, 5
>>> x
[1, 2, 3, 4, 5]

You can even omit the parenthesis -- *x, = 1, 2, 3, 4, 5 works too.

链接地址: http://www.djcxy.com/p/53556.html

上一篇: Python字符串会将主要字符变成第一个逗号

下一篇: 有没有办法在不使用Python中的方括号的情况下初始化列表?