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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM