正常情况下:
# 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