python笔记:list--pop与remove的区别

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

 

posted @ 2017-09-06 17:19  ninarMing  阅读(10146)  评论(0编辑  收藏  举报