2

我在 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']]

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

4

7 回答 7

5

您可以像添加任何其他列表一样附加或插入它们,例如:

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

或者,在一行中:

list_of_lists[0].insert(0,'NEW')
于 2012-09-21T20:05:00.900 回答
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
于 2012-09-21T20:05:34.043 回答
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项目的列表,因此必须将第一ifirsts添加为第ith 行的第一列,则:

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

希望这可以帮助

于 2012-09-21T20:05:36.243 回答
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)
于 2012-09-21T20:07:27.080 回答
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]]))
于 2012-09-21T20:09:45.900 回答
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']]
于 2012-09-21T20:12:33.417 回答
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)
于 2016-08-29T03:50:38.023 回答