相关疑难解决方法(0)

在python中将一个列表插入另一个列表的语法是什么?

给出两个列表:

x = [1,2,3]
y = [4,5,6]
Run Code Online (Sandbox Code Playgroud)

语法是什么:

  1. 插入xy这样y现在看起来像[1, 2, 3, [4, 5, 6]]
  2. 插入的所有项目x进入y,使得y现在的样子[1, 2, 3, 4, 5, 6]

python list append extend

180
推荐指数
4
解决办法
24万
查看次数

使用带打印的*(splat)运算符

我经常使用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)

python python-2.7

6
推荐指数
1
解决办法
822
查看次数

标签 统计

python ×2

append ×1

extend ×1

list ×1

python-2.7 ×1