给出两个列表:
x = [1,2,3]
y = [4,5,6]
Run Code Online (Sandbox Code Playgroud)
语法是什么:
x到y这样y现在看起来像[1, 2, 3, [4, 5, 6]]?x进入y,使得y现在的样子[1, 2, 3, 4, 5, 6]?我经常使用Python的print语句来显示数据.是的,我知道'%s %d' % ('abc', 123)方法,'{} {}'.format('abc', 123)方法和' '.join(('abc', str(123)))方法.我也知道splat operator(*)可以用来将iterable扩展为函数参数.但是,我似乎无法用print声明做到这一点.使用列表:
>>> l = [1, 2, 3]
>>> l
[1, 2, 3]
>>> print l
[1, 2, 3]
>>> '{} {} {}'.format(*l)
'1 2 3'
>>> print *l
File "<stdin>", line 1
print *l
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
使用元组:
>>> t = (4, 5, 6)
>>> t
(4, 5, 6)
>>> print t
(4, 5, 6)
>>> '%d %d …Run Code Online (Sandbox Code Playgroud)