list--pop与remove的区别
# coding=utf-8fruit = ['apple', 'pear', 'banana' ]#指定索引删除
fruit.pop(0)
#符合元素删除,具体数值
fruit.remove('pear')
print(fruit)
运行结果:['banana']
########################
# 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
####################
# coding=utf-8fruit = ['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
页:
[1]