【Day 6 of Learning Python 】修改、添加和删除列表元素

6.1  修改元素

在实际应用时,我们所创建列表的元素是动态可变的,例如,你在玩王者,击杀一人时,你的战绩位置需要更新,这时,我们的人数统计列表中的元素需要修改。

修改的方法为先找到该元素,再指定该元素的新值。

例如:我们对列表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])

结果:

D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py"
xiao xue
xiao pang

Process finished with exit code 0

6.2 添加元素

班级中增加新同学时,我们需要对列表添加新的元素,根据添加元素位置的不同,Python提供了以下几种添加的方式。

1. 在列表末尾添加元素

我们使用的方法是append(),具体操作如下:

# 在列表末尾添加元素
students = ['xiao xue', 'liu xing', 'xiao yu']
print(students)
students.append('xiao pang')
print(students)

结果:

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 0

2. 在列表中插入元素

我们使用的方法是insert(),具体操作如下:

# 在列表中插入元素
students = ['xiao xue', 'liu xing', 'xiao yu']
print(students)
students.insert(1, 'xiao pang')
print(students)

结果:

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 0

6.3 删除元素

班级中转出同学时,我们需要对列表删除对应的元素,根据删除元素位置的不同,Python提供了以下几种删除的方式。

1. 使用del()方法删除

条件:当我们知道需要删除元素在列表中的位置时,我们使用del()方法。

# 在列表中删除元素
students = ['xiao xue', 'liu xing', 'xiao yu']
print(students)
del students[1]
print(students)

结果:

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 0

2. 使用pop()方法删除

某些时候,我们需要将某一元素从一个列表中删除,放入其他列表中,例如在统计时,将一个元素从未登记某个信息的列表中删除,并加入到登记过此信息的列表中,这时,我们可以使用pop()方法。例如:

# 在列表中删除元素
students = ['xiao xue', 'liu xing', 'xiao yu']
print(students)  # 打印原列表
poped_students = students.pop()  # 使用pop()方法删除
print(poped_students)  # 打印需要删除的元素
print(students)  # 打印删除后的列表

结果:

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() 函数怎么删除指定位置的元素呢?其实,我们只需要在()内指定相应的元素的位置即可。例如:

# 在列表中删除元素
students = ['xiao xue', 'liu xing', 'xiao yu']
print(students)  # 打印原列表
poped_students = students.pop(1)  # 使用pop()方法删除位置为1的元素
print(poped_students)  # 打印需要删除的元素
print(students)  # 打印删除后的列表

结果:

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 0

3. 使用remove()方法删除

如果我们不知道需要删除的元素的具体位置,但知道需要删除的元素的值,我们可以使用remove()的方法,例如:

# 在列表中删除元素
students = ['xiao xue', 'liu xing', 'xiao yu']
print(students)  # 打印原列表
students.remove('liu xing')  # 使用remove()方法删除'liu xing'的元素
print(students)  # 打印删除后的列表

结果:

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 0

6.4 练习

将你所养的三只宠物创建一个列表:

1、打印列表中的元素

2、列表中加入第四只宠物

3、去掉第三号宠物

解答:

1、

#  练习
my_pets = ['dog', 'cat', 'parrot']  # 创建列表
print(my_pets)  # 打印列表元素

结果:

D:\Python\python.exe "F:/py practice/day ady up/Day 6 of Learning Python.py"
['dog', 'cat', 'parrot']

Process finished with exit code 0

2、

#  练习
my_pets = ['dog', 'cat', 'parrot']  # 创建列表
print(my_pets)  # 打印列表元素
my_pets.append('fish')  # 增加元素
print(my_pets)  # 打印新列表

结果:

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']  # 创建列表
print(my_pets)  # 打印列表元素
"""
my_pets.append('fish')  # 增加元素
print(my_pets)  # 打印新列表
"""
my_pets.insert(1, 'fish')  # 在第1号元素位置处添加新的元素
print(my_pets)  # 打印新的列表

结果:

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 0

3、

#  练习
my_pets = ['dog', 'cat', 'parrot']  # 创建列表

poped_my_pets = my_pets.pop(2)
print(poped_my_pets)
print(my_pets)

结果:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值