今天爱分享给大家带来Python中如何利用下标从列表中删除一个元素【面试题详解】,希望能够帮助到大家。
1.del
In [9]: a = range(10) In [10]: a Out[10]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [11]: del a[-1] In [12]: a Out[12]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
2.pop
a = ['a', 'b', 'c', 'd'] a.pop(1) # now a is ['a', 'c', 'd'] a = ['a', 'b', 'c', 'd'] a.pop() # now a is ['a', 'b', 'c']