正常情况下:
# coding=utf-8

fruit = ['apple', 'pear', 'banana' ]
#指定索引删除
fruit.pop(0)
#符合元素删除,具体数值
fruit.remove('pear')
print(fruit)



运行结果:
['banana']

反例:

pop用指定元素:
# coding=utf-8

fruit = ['apple', 'pear', 'banana' ]
fruit.pop('apple')

运行结果:
Traceback (most recent call last):
  File "...XXX.py", line 5, in <module>
    fruit.pop('apple')
TypeError: 'str' object cannot be interpreted as an integer

 remove用索引:

# coding=utf-8

fruit = ['apple', 'pear', 'banana' ]
fruit.remove(0)
print(fruit)

运行结果:
Traceback (most recent call last):
  File "...XXX.py", line 4, in <module>
    fruit.remove(0)
ValueError: list.remove(x): x not in list

 

相关文章:

  • 2021-11-27
  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-29
  • 2021-07-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案