向二维数组追加一列

3
我在Python中有一个名为"AllLines"的二维数组。
[['Suppliers', 'Spend', 'Test Field\n'], 
 ['Dell Inc', '9000', '1\n'], 
 ['Dell Computers', '9000', '2\n'], 
 ['HBC Corp', '9000', '3\n'], 
 ['HBC INC', '9000', '4']]

所以,这是一个数组内嵌另一个数组。我需要向内部的数组添加项目。以便给我这个:

[['NEW','Suppliers', 'Spend', 'Test Field\n'], 
 ['N-E-W','Dell Inc', '9000', '1\n'], 
 ['N-E-W---','Dell Computers', '9000', '2\n'], 
 ['N-E---W','HBC Corp', '9000', '3\n'], 
 ['N-W-W','HBC INC', '9000', '4']]

如何实现向内部数组添加新项目?

你的2D数组缺少每个闭合括号。 - mgilson
谢谢,那只是输出数组的结果,不是代码。数组本身是有效的,我已经测试过了。我只需要将项目附加到内部数组中。 - fredy kruger
7个回答

5
您可以像操作其他列表一样,在其中附加或插入内容,例如:
lst = list_of_lists[0]
lst.insert(0,'NEW')

或者说,简洁地概括:

list_of_lists[0].insert(0,'NEW')

我认为这个解决方案将一个项目添加到了外部数组。我需要将此项目添加到内部数组中。>>> a [0] [0:0] = ["NEW"] >>> a [1] [0:0] = ["N-E-W"] 看起来可以运行。 - fredy kruger
@fredykruger -- 你能解释一下你的意思吗? - mgilson
@fredykruger -- 这个应该做同样的事情。a[0].insert(0,'NEW') - mgilson

4
您可以使用切片赋值:
>>> a = [['Suppliers', 'Spend', 'Test Field\n'], ['Dell Inc', '9000', '1\n']]
>>> a[0][0:0] = ["NEW"]
>>> a[1][0:0] = ["N-E-W"]
>>> a
[['NEW', 'Suppliers', 'Spend', 'Test Field\n'], ['N-E-W', 'Dell Inc', '9000', '1\n']]

一些时间安排:

>>> timeit.timeit(setup="a = [['Suppliers', 'Spend', 'Test Field'], ['Dell Inc', '9000', '1']]", 
    stmt="a[0][0:0] = ['NEW']", number=100000)
3.57850867468278
>>> timeit.timeit(setup="a = [['Suppliers', 'Spend', 'Test Field'], ['Dell Inc', '9000', '1']]", 
    stmt="a[0].insert(0, 'NEW')", number=100000)
4.941971139085055
>>> timeit.timeit(setup="a = [['Suppliers', 'Spend', 'Test Field'], ['Dell Inc', '9000', '1']]", 
    stmt="a[0] = ['NEW'] + a[0]", number=100000)
33.147023662906804

我从未想过使用切片来插入单个元素。不知道与list.insert相比是否有什么好处或缺点... - mgilson
@mgilson:它明显更快。 - Tim Pietzcker
真的吗?我本来以为它会相等或者更慢,因为insert是一个更受限制的情况。非常奇怪。 - mgilson
这正是我一直在寻找的。很棒的解决方案,Tim。感谢所有抽出时间回答的人。 - fredy kruger
记录一下,这些时间测试是在Python 3.2.3,64位(Win 7)上完成的。@mgilson在OSX 10.5.8上的Python 2.7上发现了类似的结果。请查看他出色的后续问题此处 - Tim Pietzcker

2
AllLines = [['Suppliers', 'Spend', 'Test Field\n'], ['Dell Inc', '9000', '1\n'], ['Dell Computers', '9000', '2\n'], ['HBC Corp', '9000', '3\n'], ['HBC INC', '9000', '4']

在每一行的开头添加“NEW”:

newAllLines = [['NEW']+row for row in AllLines]

如果您有一个名为firsts的列表,其中每个项目的第i项必须添加为第i行的第一列,则:
newAllLines = [list(i[0])+i[1] for i in zip(firsts, AllLines)]

希望这能有所帮助。

使用 + 进行列表拼接非常慢。请查看我的回答中的时间。 - Tim Pietzcker

1
>>> lis=[['Suppliers', 'Spend', 'Test Field\n'], ['Dell Inc', '9000', '1\n'], ['Dell Computers', '9000', '2\n'], ['HBC Corp', '9000', '3\n'], ['HBC INC', '9000', '4']]
>>> lis1=['NEW','N-E-W','N-E-W---','N-E---W','N-W-W']
>>> for i,x in enumerate(lis1):
    lis[i].insert(0,x)


>>> lis
[['NEW', 'Suppliers', 'Spend', 'Test Field\n'], ['N-E-W', 'Dell Inc', '9000', '1\n'], ['N-E-W---', 'Dell Computers', '9000', '2\n'], ['N-E---W', 'HBC Corp', '9000', '3\n'], ['N-W-W', 'HBC INC', '9000', '4']]

或者像 @mgilson 建议的那样:

for item,lst in zip(lis1,lis): 
    lst.insert(0,item)

在这种情况下,我可能会使用 for item,lst in zip(lis1,lis): lst.insert(0,item) - mgilson

0

你可以对数组进行压缩。

array = [[1, 2, 3, 4],
         [6, 7, 8, 9],
         [11, 12, 13, 14],
         [16, 17, 18, 19]]

array = zip(*array)
array[0:0] = [["0", "5", "10", "15"]]
array = zip(*array)

0
>>> d=[['Suppliers', 'Spend', 'Test Field\n'],
...  ['Dell Inc', '9000', '1\n'],
...  ['Dell Computers', '9000', '2\n'],
...  ['HBC Corp', '9000', '3\n'],
...  ['HBC INC', '9000', '4']]
>>> d2 = zip(*d)
>>> d2.append([1,2,3,4,5])
>>> print zip(*d2)
[('Suppliers', 'Spend', 'Test Field\n', 1), ('Dell Inc', '9000', '1\n', 2), ('De
ll Computers', '9000', '2\n', 3), ('HBC Corp', '9000', '3\n', 4), ('HBC INC', '9
000', '4', 5)]

或者你可以把它缩短一点

print zip(*(zip(*d)+[[1,2,3,4,5]]))

这个答案依赖于zip返回一个list,而在py3k中这种方式已经不再适用。 - mgilson
zip是返回列表还是元组? - Joran Beasley

0
In [33]: lol = [['Suppliers', 'Spend', 'Test Field\n'], 
 ['Dell Inc', '9000', '1\n'], 
 ['Dell Computers', '9000', '2\n'], 
 ['HBC Corp', '9000', '3\n'], 
 ['HBC INC', '9000', '4']]

In [34]: [line.insert(0, "NEW") for line in lol]


In [35]: lol
Out[35]: 
[['NEW', 'Suppliers', 'Spend', 'Test Field\n'],
 ['NEW', 'Dell Inc', '9000', '1\n'],
 ['NEW', 'Dell Computers', '9000', '2\n'],
 ['NEW', 'HBC Corp', '9000', '3\n'],
 ['NEW', 'HBC INC', '9000', '4']]

呃,不要在副作用上使用列表推导式。 - mgilson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接