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