2012-09-21 83 views
1

我有在Python 2D阵列称为“AllLines”附加一个列到2维阵列

[['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']] 

我如何实现添加一个新的项目内部数组?

+0

您错过了每个2D阵列的右括号。 – mgilson

+0

谢谢,如果我打印数组,那只是输出,它不是代码。数组本身起作用,我测试了它。我只需要将项目追加到内部数组中。 –

回答

3

您可以使用一个切片赋值:

>>> 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 
+0

我从来没有想过使用像这样的切片来插入单个项目。我想知道是否有任何好处/弊端与'list.insert'相比... – mgilson

+0

@mgilson:它的测量速度更快。 –

+0

真的吗?我认为它会相同或更慢,因为'insert'是一个更受限制的情况。很奇怪。 – mgilson

3

您可以附加或插入它们,就像对其他任何清单如:

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

,或者在一个行:

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

我认为这个解决方案将一个项目添加到外部阵列。我需要将这个项目添加到内部数组中。 >>> a [0] [0:0] = [“NEW”] >>> a [1] [0:0] = [“NEW”] 似乎正在工作 –

+0

@fredykruger - 您能解释一下你是这个意思吗? – mgilson

+0

@fredykruger - 这应该做同样的事情。 'a [0] .insert(0,'NEW')' – mgilson

1
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'] 

“新建”添加到每个行的开头:

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

如果你有一个叫做项目firsts列表,使得firstsi个项必须添加为第一在i第i行的列,则:

newAllLines = [list(i[0])+i[1] for i in zip(firsts, AllLines)] 

希望这有助于

+0

使用'+'列表连接非常缓慢。看到我的答案中的时间。 –

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) 
+0

在这种情况下,我可能会为'item,lst in zip(lis1,lis):lst.insert(0,item)' – mgilson

+0

是啊!那更好。 –

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]])) 
+0

这个答案依赖'zip'返回一个'list',它在'py3k'中消失。 – mgilson

+0

zip确实或只是列表(元组来代替?) –

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']] 
+0

Yuck。不要使用list-comp进行副作用。 – 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)