Skip to main content
 首页 » 编程设计

python之Python 中的列表操作之增加元素列表

2025年05月04日9think

我定义:

A = [[1, 2], [3, 4], [5, 6]] 

生产线生产什么:

B = [['hello', [1, 2]], ['hello', [3, 4]], ['hello', [5, 6]]] 

请您参考如下方法:

您可以使用列表理解将 'hello' 添加到每个列表的前面:

>>> add = 'hello' 
>>> A = [[1, 2], [3, 4], [5, 6]] 
>>> [[add, x] for x in A] 
[['hello', [1, 2]], ['hello', [3, 4]], ['hello', [5, 6]]] 
# or [[add] + [x] for x in A]