在Python中将数据添加到嵌套列表 [英] Adding data to a nested list in Python

查看:363
本文介绍了在Python中将数据添加到嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套列表,例如:

I have a nested list e.g.:

nlist = [
         [1, 2, 3], 
         [4, 5, 6], 
         [7, 8, 9],
        ]

在将此列表插入数据库之前,我想向其添加一个列",并在新列的每一行中添加相同的值,例如:

Before I insert this list into a database, I would like to add a "column" to it with the same value in each row of the new column e.g:

nlist = [
            [a, 1, 2, 3], 
            [a, 4, 5, 6], 
            [a, 7, 8, 9],
           ]

例如,当原始的嵌套列表可能包含数百行时,什么是最好的方法?

What's the best way to do this, when, for example, the original nested list might have hundreds of rows?

推荐答案

为什么不更改原始列表(如果您要这样做的话):

Why not change the original list (if that is all you want to do):

for row in nlist:
    row.insert(0, a)

这篇关于在Python中将数据添加到嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆