【问题标题】:Append a column to a 2 dimensional array将一列附加到二维数组
【发布时间】:2012-09-21 20:01:19
【问题描述】:

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

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

【问题讨论】:

  • 每个二维数组都缺少右括号。
  • 谢谢,如果我打印数组,那只是输出,不是代码。数组本身有效,我测试了它。我只需要将项目附加到内部数组。

标签: python


【解决方案1】:

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

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

或者,在一行中:

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

【讨论】:

  • 我认为这个解决方案将一个项目添加到外部数组。我需要将此项目添加到内部数组中。 >>> a[0][0:0] = ["NEW"] >>> a[1][0:0] = ["N-E-W"] 似乎有效
  • @fredykruger -- 你能解释一下你的意思吗?
  • @fredykruger -- 这应该做同样的事情。 a[0].insert(0,'NEW')
【解决方案2】:

你可以使用切片赋值:

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

【讨论】:

  • 我从来没有想过使用这样的切片来插入单个项目。我想知道与list.insert相比是否有任何优点/缺点......
  • @mgilson:明显更快。
  • 真的吗?我希望它等于或慢,因为insert 是一个更受限制的情况。很奇怪。
  • 这正是我想要的。很好的解决方案,蒂姆。感谢所有花时间回答的人。
  • 作为记录,计时是在 Python 3.2.3、64 位(Win 7)上完成的。 @mgilson 在 OSX 10.5.8 上的 Python 2.7 上发现了类似的结果。查看他出色的后续问题here。
【解决方案3】:
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 的项目列表,因此必须将 firsts 的 ith 项目添加为 ith 行的第一列,那么:

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

希望对你有帮助

【讨论】:

  • 使用+ 的列表连接非常慢。请参阅我的答案中的时间安排。
【解决方案4】:
>>> 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)

【讨论】:

  • 在这种情况下,我可能会做for item,lst in zip(lis1,lis): lst.insert(0,item)
【解决方案5】:
>>> 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]]))

【讨论】:

  • 这个答案依赖于zip 返回一个list,它在py3k 中消失。
  • zip 做或只是列表(而不是元组?)
【解决方案6】:
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']]

【讨论】:

  • 呸。不要使用 list-comp 来产生副作用。
【解决方案7】:

你可以压缩数组。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2016-03-18
    • 2019-04-22
    • 2019-03-22
    相关资源
    最近更新 更多