替换嵌套列表中某些列表中的值
Replacing values in certain list in nested lists
我正在为蜡笔制造商编写程序。他们有 4 包库存(目前是调试的良好起点)。我已经在列表中显示:
colors=[['pink','green','blue'],['blue','green','red'],['pink',blue','yellow'],['orange','pink','yellow']]
我想改变中间绿色的两包蜡笔的颜色,以获得颜色更多样的包。
首先我找到所有中间有绿色的包:
packsfound = []
for pack in colors:
if pack[1]=="green":
packsfound.append(pack)
print("packs with one:",packsfound)
然后我从库存(颜色)中取出我选择的包装,以便稍后可以对其进行修改和放回。
try:
for pack in packsfound:
colors.remove(pack)
except Exception as e:
print(e)
pass
然后我做替换:
for pack in packsfound:
try:
for i, color in enumerate(packsfound):
position=color.index("green")
print("replace at:",position)
break
pack[position]="yellow"
print("replaced rows:",packsfound)
except Exception as e:
print(e)
pass
然后我将修改后的列表追加回颜色,以便它们是新的库存
try:
for pack in packsfound:
colors.append(pack)
except Exception as e:
print(e)
pass
print(colors)
问题是它只遍历第一个列表并替换第一个绿色。然后程序说绿色不在列表中并且不会替换第二个绿色:
packs with one: [['pink', 'green', 'blue'], ['blue', 'green', 'red']]
replace at: 1
replaced rows: [['pink', 'yellow', 'blue'], ['blue', 'green', 'red']]
'green' is not in list
[['pink', 'blue', 'yellow'], ['orange', 'pink', 'yellow'], ['pink', 'yellow', 'blue'], ['blue', 'green', 'red']]
我已经尝试了很多东西,比如移动 try 和 except 以及将替换行移入和移出循环,添加 break 和 pass 但没有任何效果。
与其更改列表中的元素,不如构建一个新列表更容易 return。使用简单的 list comprehension
和 if/else
:
>>> mid = int( len(colors[0])/2 )
>>> [ color[:mid]+['yellow']+color[mid+1:] if color[mid]=='green' else color for color in colors ]
=> [['pink', 'yellow', 'blue'], ['blue', 'yellow', 'red'], ['pink', 'blue', 'yellow'], ['orange', 'pink', 'yellow']]
操作说明: 这是考虑到您只想用 green
替换 [=14] 中的颜色=] 与 yellow
。否则,所有 'green' 元素需要替换为 yellow
,使用 replace.
OP 注意事项: 这是在一组颜色为奇数的情况下。否则偶数的中间是两个元素,所以相应地修改代码。
替换循环有错误。你放置 break 语句的方式只允许循环的一次迭代。您可以使用此循环:
for pack in packsfound:
try:
position = pack.index('green')
print("replace at:",position)
pack[position]="yellow"
print("replaced rows:",packsfound)
except Exception as e:
print(e)
pass
我认为您的替换步骤是问题所在。当您尝试枚举嵌套列表时,您似乎犯了一个错误。我不确定为什么您已经在使用 .index()
来获取要搜索的颜色值的索引时还要尝试枚举列表。
packsfound = [['pink', 'green', 'blue'], ['blue', 'green', 'red']]
for pack in packsfound:
try:
for color in pack:
position = pack.index('green')
print("replace at: ", position)
break
pack[position] = 'yellow'
print("replaced rows: ", packsfound)
except Exception as e:
print(e)
pass
请原谅我更改了变量名。这输出:
replace at: 1
replaced rows: [['pink', 'yellow', 'blue'], ['blue', 'green', 'red']]
replace at: 1
replaced rows: [['pink', 'yellow', 'blue'], ['blue', 'yellow', 'red']]
我正在为蜡笔制造商编写程序。他们有 4 包库存(目前是调试的良好起点)。我已经在列表中显示:
colors=[['pink','green','blue'],['blue','green','red'],['pink',blue','yellow'],['orange','pink','yellow']]
我想改变中间绿色的两包蜡笔的颜色,以获得颜色更多样的包。 首先我找到所有中间有绿色的包:
packsfound = []
for pack in colors:
if pack[1]=="green":
packsfound.append(pack)
print("packs with one:",packsfound)
然后我从库存(颜色)中取出我选择的包装,以便稍后可以对其进行修改和放回。
try:
for pack in packsfound:
colors.remove(pack)
except Exception as e:
print(e)
pass
然后我做替换:
for pack in packsfound:
try:
for i, color in enumerate(packsfound):
position=color.index("green")
print("replace at:",position)
break
pack[position]="yellow"
print("replaced rows:",packsfound)
except Exception as e:
print(e)
pass
然后我将修改后的列表追加回颜色,以便它们是新的库存
try:
for pack in packsfound:
colors.append(pack)
except Exception as e:
print(e)
pass
print(colors)
问题是它只遍历第一个列表并替换第一个绿色。然后程序说绿色不在列表中并且不会替换第二个绿色:
packs with one: [['pink', 'green', 'blue'], ['blue', 'green', 'red']]
replace at: 1
replaced rows: [['pink', 'yellow', 'blue'], ['blue', 'green', 'red']]
'green' is not in list
[['pink', 'blue', 'yellow'], ['orange', 'pink', 'yellow'], ['pink', 'yellow', 'blue'], ['blue', 'green', 'red']]
我已经尝试了很多东西,比如移动 try 和 except 以及将替换行移入和移出循环,添加 break 和 pass 但没有任何效果。
与其更改列表中的元素,不如构建一个新列表更容易 return。使用简单的 list comprehension
和 if/else
:
>>> mid = int( len(colors[0])/2 )
>>> [ color[:mid]+['yellow']+color[mid+1:] if color[mid]=='green' else color for color in colors ]
=> [['pink', 'yellow', 'blue'], ['blue', 'yellow', 'red'], ['pink', 'blue', 'yellow'], ['orange', 'pink', 'yellow']]
操作说明: 这是考虑到您只想用 green
替换 [=14] 中的颜色=] 与 yellow
。否则,所有 'green' 元素需要替换为 yellow
,使用 replace.
OP 注意事项: 这是在一组颜色为奇数的情况下。否则偶数的中间是两个元素,所以相应地修改代码。
替换循环有错误。你放置 break 语句的方式只允许循环的一次迭代。您可以使用此循环:
for pack in packsfound:
try:
position = pack.index('green')
print("replace at:",position)
pack[position]="yellow"
print("replaced rows:",packsfound)
except Exception as e:
print(e)
pass
我认为您的替换步骤是问题所在。当您尝试枚举嵌套列表时,您似乎犯了一个错误。我不确定为什么您已经在使用 .index()
来获取要搜索的颜色值的索引时还要尝试枚举列表。
packsfound = [['pink', 'green', 'blue'], ['blue', 'green', 'red']]
for pack in packsfound:
try:
for color in pack:
position = pack.index('green')
print("replace at: ", position)
break
pack[position] = 'yellow'
print("replaced rows: ", packsfound)
except Exception as e:
print(e)
pass
请原谅我更改了变量名。这输出:
replace at: 1
replaced rows: [['pink', 'yellow', 'blue'], ['blue', 'green', 'red']]
replace at: 1
replaced rows: [['pink', 'yellow', 'blue'], ['blue', 'yellow', 'red']]