python - 在列表中的每个项目之后插入一个项目(Python)

标签 python list insert

假设我有一个这样的列表:

a = [[1, 2, 3], [4, 5, 6]]
filler = ['cat']

我怎么得到

b = [[1 ,2 ,3], ['cat'], [4, 5, 6], ['cat']] 

作为输出?

最佳答案

我更喜欢将 itertools 用于这样的事情:

>>> import itertools as it
>>> a = [[1, 2, 3], [4, 5, 6]]
>>> filler = ['cat']
>>> list(it.chain.from_iterable(it.izip(a, it.repeat(filler))))
[[1, 2, 3], ['cat'], [4, 5, 6], ['cat']]

关于python - 在列表中的每个项目之后插入一个项目(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13219981/

相关文章:

python - 使用 itertools 从列表中进行组合

python - 我如何判断 Python 的多处理模块是否正在使用我的所有内核进行计算?

python - 如何在 python 中增加 cpu 核心数量及其使用率

python-3.x - 如何按我的顺序对列表进行多次排序?

php - 如何使用 PHP 一次将数据插入两个 mysql 表中

python - KeyError:[列] 中没有 Float64Index,不确定如何进行

Python:合并字典列表

python - 查找具有重复项的索引

excel - 插入新行 VBA 宏

PHP MySQLi 插入获取最后一列值加 1