有许多方法来替换 Python 列表中的一个或多个项目。我们可以使用索引、列表理解、for 循环和 map() 函数,来改变 Python 列表中的单个或多个值。
可能有很多情况下,你想从一个 Python 列表中替换单个或多个元素。在这种情况下,你必须知道所有的技术才能做到这一点。在这个 Python 教程中,我们将通过不同的方法来替换或改变 Python 列表中的一个元素。
概述
下面是我们将学习的从 Python 列表中改变单个或多个值的技术列表。
- 列表索引和分片。这是访问和改变 Python 列表值的基本方法。
- 列表理解。它是一种速记技术,使用单行 Python 语句从现有列表中创建一个新的列表。
- **For 循环:**使用 for 循环,我们可以遍历列表中的元素,并寻找我们想要改变的不同元素。
- **map 函数:**在 map 函数的帮助下,我们可以在 Python 列表的每个元素上应用一个函数,并替换列表元素的值。
Python 替换列表元素。使用列表的索引和切分
Python 列表支持索引和切片,它允许我们从 Python 列表中访问单个或一系列的元素。
不仅是访问,我们还可以使用索引和切片操作,用赋值运算符给现有的列表元素添加新的值。
在 Python 列表索引的帮助下,我们可以访问和替换一个列表元素。
例如,假设我们有一个Python列表recharge_plans ,其中包含不同充值计划的价格。
recharge_plans = [149, 250, 399, 599, 999]
现在由于商业策略的一些变化,公司决定将第一个计划的价格提高10% 。在这种情况下,现在你需要改变第一个元素的价格,要做到这一点,最好的方法是使用Python列表索引。
recharge_plans = [149, 250, 399, 599, 999]
#replace the first element of the recharge_plan using indexing
recharge_plans[0] = recharge_plans[0] + (10*recharge_plans[0])/100
print(recharge_plans)
输出
[163.9, 250, 399, 599, 999]
我们也可以使用列表分片法来替换Python列表中的多个元素。
例子
现在让我们使用列表分片法将recharge_plans的前3个元素的价格提高10%。
recharge_plans = [149, 250, 399, 599, 999]
print("Initial Recharge Plans: ", recharge_plans)
# new value for first element
first =recharge_plans[0] + (10*recharge_plans[0])/100
# new value for second element
second =recharge_plans[1] + (10*recharge_plans[1])/100
# new value for third element
third = recharge_plans[2] + (10*recharge_plans[2])/100
#replace the firt three elements of the recharge_plan using slicing
recharge_plans[0:3] = [first, second, third]
print("Recharge Plans After 10% increment: ",recharge_plans)
输出
Initial Recharge Plans: [149, 250, 399, 599, 999]
Recharge Plans After 10% increment: [163.9, 275.0, 438.9, 599, 999]
尽管通过列表切片,我们可以替换多个元素,但我们仍然可以使用重复的逻辑来创建新的值,就像我们在上面的例子中做的那样。增加10%增量的逻辑是相同的,但我们在将这些值分配给列表切片时,需要将它们存储在一个列表中。
所以在Python列表中替换多个值时,使用for循环、列表理解或map()函数总是一个更好的方法。
Python 替换列表元素。使用 List Comprehension
列表理解提供了一个单行代码语法,可以从现有列表中创建一个新的列表。当我们希望从一个 Python 列表中替换多个或所有的元素时,我们也可以使用列表理解。
例子 1
在这个例子中,我们将使用列表理解并替换我们的recharge_plans 中的所有元素。
recharge_plans = [149, 250, 399, 599, 999]
print("Initial Recharge Plans: ", recharge_plans)
'''
replace all the elements from the recharge_plane list using list comprehension
with 10% increment in every element value
'''
recharge_plans = [(element+(element*10)/100) for element in recharge_plans]
print("Recharge Plans After 10% increment: ",recharge_plans)
输出
Initial Recharge Plans: [149, 250, 399, 599, 999]
Recharge Plans After 10% increment: [163.9, 275.0, 438.9, 658.9, 1098.9]
在上面的例子中,我们使用列表理解将recharge_plans 中的每个元素都替换为 10% 的增量。
但是如果我们只想替换特定的元素,我们也可以使用if..else 语句与列表理解。
语法
list_name = [new_element if condition else new_element for index, element enumerate(exist_list) ]
例2
让我们使用列表理解,只替换recharge_plans 列表中的第二和第三元素。要做到这一点,我们还需要使用 Pythonenumerate,它将帮助我们访问元素的索引值。
recharge_plans = [149, 250, 399, 599, 999]
print("Initial Recharge Plans: ", recharge_plans)
'''
replace only 1st and 2nd elements from the recharge_plane list using list comprehension
with 10% increment
'''
recharge_plans = [(element+(element*10)/100) if index == 0 or index==1 else element for index, element in enumerate(recharge_plans) ]
print("Recharge Plans After 10% increment: ",recharge_plans)
输出
Initial Recharge Plans: [149, 250, 399, 599, 999]
Recharge Plans After 10% increment: [163.9, 275.0, 399, 599, 999]
Python替换列表中的元素。使用 For Loop
尽管使用列表理解,我们可以从一个列表中替换多个元素,但是在有多个条件的情况下,语句可能会被集中起来,很难一次就理解。
所以当我们有多个条件时,我们应该选择使用for循环而不是列表理解。
使用for 循环,我们可以遍历列表中的元素,但是当涉及到改变或替换数值时,我们需要元素的索引号。所以在 Python 中使用索引循环,我们使用 enumerate 函数。
例子
recharge_plans = [149, 250, 399, 599, 999]
print("Initial Recharge Plans: ", recharge_plans)
'''
replace only 1st and 2nd elements from the recharge_plane list using for loop
with 10% increment.
'''
for index, element in enumerate(recharge_plans):
# replace the value of 0 and 1 index position elements
# by incresing them 10%
if index in (0,1):
recharge_plans[index] = element+ (element*10)/100
print("Recharge Plans After 10% increment: ",recharge_plans)
输出
Initial Recharge Plans: [149, 250, 399, 599, 999]
Recharge Plans After 10% increment: [163.9, 275.0, 399, 599, 999]
Python 替换列表中的元素。使用Python地图函数
使用Python map函数,我们可以对所有的Python列表元素执行类似的操作。
我们可以利用Python map函数的这个属性,从Python列表中替换特定的或所有的元素。
例子
现在让我们使用map函数,将recharge_plans 的第3和第4个值增加10%来替换。
def increment_10(index, element):
# replace the value of 2 and 3 index position elements
# by increasing them 10%
if index in (2,3):
return element+ (element*10)/100
# if the index value is not 2 or 3
# value will remain the same
return element
recharge_plans = [149, 250, 399, 599, 999]
indices = range(len(recharge_plans))
print("Initial Recharge Plans: ", recharge_plans)
'''
replace only 3rd and 4th elements from the recharge_plane list using map
with 10% increment.
'''
recharge_plans = list(map(increment_10, indices, recharge_plans))
print("Recharge Plans After 10% increment: ",recharge_plans)
输出
Initial Recharge Plans: [149, 250, 399, 599, 999]
Recharge Plans After 10% increment: [149, 250, 438.9, 658.9, 999]
总结!
在本教程中,我们学习了如何使用不同的技术来替换 Python 中的列表元素。通过索引、切片和 for 循环技术,我们在不创建新列表的情况下替换了列表元素,在这些技术中,我们进行了原地替换列表元素的操作。但是使用list comprehension和map()函数,我们使用现有的列表创建一个新的列表,并将新的列表值分配给现有的列表名称,这也是一种技巧,但是在幕后,我们正在创建一个新的列表,使用额外的空间。