Han*_*Sun 3 python functional-programming
有没有办法在Python中不使用方括号来初始化列表?
例如,是否有这样的函数list_cons:
x = list_cons(1, 2, 3, 4)
Run Code Online (Sandbox Code Playgroud)
相当于:
x = [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
Lev*_*sky 11
In [1]: def list_cons(*args):
...: return list(args)
...:
In [2]: list_cons(1,2,3,4)
Out[2]: [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
我认为这不是一个特别有用的功能.输入括号这么难吗?如果您解释为什么要这样做,也许我们可以给您一个更有用的答案.
不过,在Python 3中你可以做一些有趣的事情:
>>> (*x,) = 1, 2, 3, 4, 5
>>> x
[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
你甚至可以省略括号 - *x, = 1, 2, 3, 4, 5也可以.
| 归档时间: |
|
| 查看次数: |
244 次 |
| 最近记录: |