【问题标题】:Removing a sublist from a main list without knowing its position in the list [duplicate]从主列表中删除子列表而不知道其在列表中的位置[重复]
【发布时间】:2017-01-24 16:01:30
【问题描述】:

假设我有一个随机生成的子列表的二维列表,其中包含这样的随机数

list = [(1,2),(2,3),(3,4),(4,5)]

如果我不知道子列表“(2,3)”在列表中的位置,我该如何从主列表中删除它? 我尝试使用 .pop 但在不知道子列表在列表中的位置的情况下无法使其工作。

【问题讨论】:

  • lst.remove((2,3)) 应该这样做。

标签: python arrays list


【解决方案1】:

remove 函数可能是您正在寻找的: https://www.tutorialspoint.com/python/list_remove.htm

list = [(1,2),(2,3),(3,4),(4,5)]
list.remove((2,3))
print(list) # prints [(1, 2), (3, 4), (4, 5)] in python 3

不过,我建议您将变量重命名为 list 以外的名称,因为那是 python 关键字。

【讨论】:

  • 不要调用你的变量list
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 2021-05-19
  • 2018-04-22
  • 2011-01-13
相关资源
最近更新 更多