Python 将列附加到二维数组
我在Python中有一个名为“AllLines”的2d数组 因此,它是数组中的一个数组。我需要将项目附加到内部数组中。给我这个:Python 将列附加到二维数组,python,Python,我在Python中有一个名为“AllLines”的2d数组 因此,它是数组中的一个数组。我需要将项目附加到内部数组中。给我这个: [['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'
[['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']]
如何将新项目添加到内部阵列?您可以像添加任何其他列表一样添加或插入到阵列中,例如:
lst = list_of_lists[0]
lst.insert(0,'NEW')
或者,在一行中:
list_of_lists[0].insert(0,'NEW')
您可以使用切片分配:
>>> 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
要将“新建”添加到每行的开头,请执行以下操作:
newAllLines = [['NEW']+row for row in AllLines]
如果您有一个名为firsts
的项目列表,因此必须将firsts
中的i
th项目添加为i
第th行的第一列,则:
newAllLines = [list(i[0])+i[1] for i in zip(firsts, AllLines)]
希望这有帮助
>>> 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)
或者你可以缩短它
print zip(*(zip(*d)+[[1,2,3,4,5]]))
您可以压缩阵列
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)
每个2D数组都缺少一个右括号。谢谢,如果我打印数组,这只是输出,不是代码。阵列本身可以工作,我对它进行了测试。我只需要将项目附加到内部数组中。我从未想过使用这样的切片来插入单个项目。我想知道与
列表相比是否有任何优点/缺点。insert
。@mgilson:它的速度要快得多。真的吗?我希望它是相等的或较慢的,因为insert
是一种更受约束的情况。很奇怪,这正是我要找的。很好的解决方案,蒂姆。感谢所有花时间回答的人。为了记录在案,计时是在Python3.2.3 64位(Win7)上完成的@mgilson在OSX 10.5.8上的Python2.7上发现了类似的结果。看看他出色的后续问题。在这种情况下,我可能会在zip中为item,lst做(lis1,lis):lst.insert(0,item)
这个答案依赖于zip
返回一个列表,该列表在py3k
中消失。zip做了,或者只是列表(而不是元组?)。恶心。不要使用列表comp来处理副作用。我认为此解决方案会在外部数组中添加一个项。我需要将此项目添加到内部阵列中。>>a[0][0:0]=[“新”]>>>a[1][0:0]=[“N-E-W”]似乎是work@fredykruger--你能解释一下你的意思吗?@fredykruger--这应该也一样<代码>a[0]。使用+
插入(0,'NEW')
列表串联速度非常慢。请看我回答中的时间安排。
>>> 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]]))
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']]
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)