python - 在嵌套列表的开头插入一个列表

标签 python list insert nested-lists

我有两个列表。一种是列表列表,另一种是标准列表。我需要以某种方式组合它们,以便以正确的格式提供给需要列表列表的 Google Sheets API。

a =[['079872', 'OE', '02', 'BG', 'K'], ['159926', 'UN', '42', 'DB', 'K'], ['159844', 'UN', '24', 'DB', 'V']]

b = ['col1', 'col2', 'col3','col4', 'col5']

c = a + b 

c 产量:

['col1', col2','col3','col4', 'col5'
['079872', 'OE', '02', 'BG', 'K'],
['159926', 'UN', '42', 'DB', 'K'],
['159844', 'UN', '24', 'DB', 'V']]

我需要的是:

[['col1', col2','col3','col4', 'col5'],
['079872', 'OE', '02', 'BG', 'K'],
['159926', 'UN', '42', 'DB', 'K'],
['159844', 'UN', '24', 'DB', 'V']]

我已经尝试了所有可以在 SO 中找到的解决方案,但无法找到我需要的列表列表。时间差

最佳答案

这个简单的解决方案也可以:

>>> [b] + a
 [['col1', 'col2', 'col3', 'col4', 'col5'],
 ['079872', 'OE', '02', 'BG', 'K'],
 ['159926', 'UN', '42', 'DB', 'K'],
 ['159844', 'UN', '24', 'DB', 'V']]

关于python - 在嵌套列表的开头插入一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45654335/

相关文章:

python - 不使用字典从字符串形成变量

python - AWS Lambda python 重置断开的连接

python - Scrapy 跟踪所有链接并获取状态

r - 根据 R 中的名称从 data.frames 列表中删除 data.frame

java - 将列表的元素分组为子列表(可能使用 Guava )

mysql - SQL更新,如果不存在则插入

python - 需要帮助思考在 Python 中以整数除法拆分列表

python - Google App Engine - 跟踪使用了哪些索引

Python 列表理解 csv 文件行提取未正确评估

mysql - SQL插入不存在的地方以防止主键问题