2

「AllLines」というPythonの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']]

内部配列に新しいアイテムを追加するにはどうすればよいですか?

4

7 に答える 7

5

他のリストと同じように、それらに追加または挿入できます。

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

または、1 行で:

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ような、 ofという名前のリストがある場合、次のようになります。ifirstsi

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 に答える