【Day 6 of Learning Python 】修改、添加和删除列表元素
6.1? 修改元素
在實際應用時,我們所創(chuàng)建列表的元素是動態(tài)可變的,例如,你在玩王者,擊殺一人時,你的戰(zhàn)績位置需要更新,這時,我們的人數(shù)統(tǒng)計列表中的元素需要修改。
修改的方法為先找到該元素,再指定該元素的新值。
例如:我們對列表students賦予元素? 'xiao xue', 'liu xing', 'xiao yu',輸出此列表;再對列表students中的 0 位元素重新賦值為 xiao pang,并輸出列表,如下:
students = ['xiao xue', 'liu xing', 'xiao yu'] print(students[0]) students[0] = 'xiao pang' print(students[0])結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" xiao xue xiao pangProcess finished with exit code 06.2 添加元素
班級中增加新同學時,我們需要對列表添加新的元素,根據(jù)添加元素位置的不同,Python提供了以下幾種添加的方式。
1. 在列表末尾添加元素
我們使用的方法是append(),具體操作如下:
# 在列表末尾添加元素 students = ['xiao xue', 'liu xing', 'xiao yu'] print(students) students.append('xiao pang') print(students)結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" ['xiao xue', 'liu xing', 'xiao yu'] ['xiao xue', 'liu xing', 'xiao yu', 'xiao pang']Process finished with exit code 02. 在列表中插入元素
我們使用的方法是insert(),具體操作如下:
# 在列表中插入元素 students = ['xiao xue', 'liu xing', 'xiao yu'] print(students) students.insert(1, 'xiao pang') print(students)結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" ['xiao xue', 'liu xing', 'xiao yu'] ['xiao xue', 'xiao pang', 'liu xing', 'xiao yu']Process finished with exit code 06.3 刪除元素
班級中轉(zhuǎn)出同學時,我們需要對列表刪除對應的元素,根據(jù)刪除元素位置的不同,Python提供了以下幾種刪除的方式。
1. 使用del()方法刪除
條件:當我們知道需要刪除元素在列表中的位置時,我們使用del()方法。
# 在列表中刪除元素 students = ['xiao xue', 'liu xing', 'xiao yu'] print(students) del students[1] print(students)結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" ['xiao xue', 'liu xing', 'xiao yu'] ['xiao xue', 'xiao yu']Process finished with exit code 02. 使用pop()方法刪除
某些時候,我們需要將某一元素從一個列表中刪除,放入其他列表中,例如在統(tǒng)計時,將一個元素從未登記某個信息的列表中刪除,并加入到登記過此信息的列表中,這時,我們可以使用pop()方法。例如:
# 在列表中刪除元素 students = ['xiao xue', 'liu xing', 'xiao yu'] print(students) # 打印原列表 poped_students = students.pop() # 使用pop()方法刪除 print(poped_students) # 打印需要刪除的元素 print(students) # 打印刪除后的列表結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" ['xiao xue', 'liu xing', 'xiao yu'] xiao yu ['xiao xue', 'liu xing']Process finished with exit code 0上面我們刪除了列表中的元素‘xiao yu’,將要刪除的元素,及刪除后的列表都顯示出來。有些同學會問,pop() 函數(shù)怎么刪除指定位置的元素呢?其實,我們只需要在()內(nèi)指定相應的元素的位置即可。例如:
# 在列表中刪除元素 students = ['xiao xue', 'liu xing', 'xiao yu'] print(students) # 打印原列表 poped_students = students.pop(1) # 使用pop()方法刪除位置為1的元素 print(poped_students) # 打印需要刪除的元素 print(students) # 打印刪除后的列表結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" ['xiao xue', 'liu xing', 'xiao yu'] liu xing ['xiao xue', 'xiao yu']Process finished with exit code 03. 使用remove()方法刪除
如果我們不知道需要刪除的元素的具體位置,但知道需要刪除的元素的值,我們可以使用remove()的方法,例如:
# 在列表中刪除元素 students = ['xiao xue', 'liu xing', 'xiao yu'] print(students) # 打印原列表 students.remove('liu xing') # 使用remove()方法刪除'liu xing'的元素 print(students) # 打印刪除后的列表結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" ['xiao xue', 'liu xing', 'xiao yu'] ['xiao xue', 'xiao yu']Process finished with exit code 06.4 練習
將你所養(yǎng)的三只寵物創(chuàng)建一個列表:
1、打印列表中的元素
2、列表中加入第四只寵物
3、去掉第三號寵物
解答:
1、
# 練習 my_pets = ['dog', 'cat', 'parrot'] # 創(chuàng)建列表 print(my_pets) # 打印列表元素結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" ['dog', 'cat', 'parrot']Process finished with exit code 02、
# 練習 my_pets = ['dog', 'cat', 'parrot'] # 創(chuàng)建列表 print(my_pets) # 打印列表元素 my_pets.append('fish') # 增加元素 print(my_pets) # 打印新列表結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" ['dog', 'cat', 'parrot'] ['dog', 'cat', 'parrot', 'fish']Process finished with exit code 0或者:
# 練習 my_pets = ['dog', 'cat', 'parrot'] # 創(chuàng)建列表 print(my_pets) # 打印列表元素 """ my_pets.append('fish') # 增加元素 print(my_pets) # 打印新列表 """ my_pets.insert(1, 'fish') # 在第1號元素位置處添加新的元素 print(my_pets) # 打印新的列表結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" ['dog', 'cat', 'parrot'] ['dog', 'fish', 'cat', 'parrot']Process finished with exit code 03、
# 練習 my_pets = ['dog', 'cat', 'parrot'] # 創(chuàng)建列表poped_my_pets = my_pets.pop(2) print(poped_my_pets) print(my_pets)結(jié)果:
D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py" parrot ['dog', 'cat']Process finished with exit code 0總結(jié)
以上是生活随笔為你收集整理的【Day 6 of Learning Python 】修改、添加和删除列表元素的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python学习之路——12306爬票遇
- 下一篇: 字母异位词(anagram)的不同复杂度