elliott.david 2009-03-09 18:16 采纳率: 25%
浏览 1323
已采纳

如何在 Python 中按索引从列表中删除元素?

How do I remove an element from a list by index in Python?

I found the list.remove method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.

转载于:https://stackoverflow.com/questions/627435/how-do-i-remove-an-element-from-a-list-by-index-in-python

  • 写回答

18条回答 默认 最新

  • 零零乙 2009-03-09 18:21
    关注

    Use del and specify the index of the element you want to delete:

    >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> del a[-1]
    >>> a
    [0, 1, 2, 3, 4, 5, 6, 7, 8]
    

    Also supports slices:

    >>> del a[2:4]
    >>> a
    [0, 1, 4, 5, 6, 7, 8, 9]
    

    Here is the section from the tutorial.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(17条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题