【问题标题】:Pop() an element of a list in the list of lists [duplicate]Pop()列表列表中的列表元素[重复]
【发布时间】:2015-11-26 03:28:40
【问题描述】:

我有一个列表列表。我想从列表列表中的第二个列表中弹出()一个元素。 这是一个例子:

>>> list1=[1,2]
>>> list2=[3,4]
>>> listoflists=[list1, list2]

所以,打印 listoflists 给了我:

>>>listoflists
[[1, 2], [3, 4]]

我想弹出 listoflists 中第二个列表的第一个元素,即 3。

>>>listoflists.pop([1][0])

给我以下错误;

Traceback (most recent call last):

  File "<ipython-input-14-db5dc303028d>", line 1, in <module>
    listoflists.pop([0][1])

IndexError: list index out of range

【问题讨论】:

  • listoflists[1].pop() ?
  • 我喜欢人们在 SO 上提问之前为解决问题所付出的努力

标签: python list


【解决方案1】:
listoflists[1].pop(0)

listoflists[1] 等于 list2

所以

listoflists[1].pop(0) 等于 list2.pop(0)

【讨论】:

  • 这行得通!谢谢。
【解决方案2】:

弹出二维数组的正确方法是这样的

list1=[1,2]
list2=[3,4]
listoflists=[list1, list2]

print listoflists

listoflists[0].pop(0)//correct way to pop

print listoflists

here 是另一个与您类似的关于弹出二维列表的帖子,也可能有用。

【讨论】:

    猜你喜欢
    • 2020-11-02
    • 1970-01-01
    • 2020-08-02
    • 2018-11-03
    • 2014-12-31
    • 2022-07-09
    • 2023-04-05
    • 1970-01-01
    • 2013-01-30
    相关资源
    最近更新 更多