我在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']]
如何在内部数组中添加新项目?
答案 0 :(得分:3)
您可以像其他任何列表一样追加或插入它们,例如:
lst = list_of_lists[0]
lst.insert(0,'NEW')
或者,在一行中:
list_of_lists[0].insert(0,'NEW')
答案 1 :(得分: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
答案 2 :(得分: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']
在每行的开头添加“NEW”:
newAllLines = [['NEW']+row for row in AllLines]
如果您有一个名为firsts
项的列表,则必须将i
firsts
项i
添加为newAllLines = [list(i[0])+i[1] for i in zip(firsts, AllLines)]
行的第一列, :
{{1}}
希望这有帮助
答案 3 :(得分: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)
答案 4 :(得分: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]]))
答案 5 :(得分: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']]
答案 6 :(得分: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)