英文:
How To Remove an Element from a List Using Another List's Placeholder in Python
问题
I need to remove a certain element from a list of dictionaries. Unfortunately, the only reference to the object needing removal is a place in another list:
enemy_list[num]
I need to take out enemy_list[num]
from everyone
.
ii = 0
for i in enemy_list:
print(ii, ':', enemy_list[ii])
ii += 1
num = input("Which of your opponents would you like to eliminate? ")
num = int(num)
del everyone[enemy_list[num]]
I cannot remove it from only enemy_list
because it is reset multiple times as everyone in list everyone
takes a turn.
我尝试过这个问题,但它只适用于从彼此中删除整个列表。有没有办法我可以改变它以适应这种情况?
感谢您的时间!
英文:
I need to remove a certain element from a list of dictionaries. Unfortunately, the only reference to the object needing removal is a place in another list:
enemy_list[num]
I need to take out enemy_list[num]
from everyone
.
ii = 0
for i in enemy_list:
print(ii, ':', enemy_list[ii])
ii += 1
num = input("Which of your opponents would you like to eliminate? ")
num = int(num)
del everyone[enemy_list[num]]
I cannot remove it from only enemy_list
because it is reset multiple times as everyone in list everyone
takes a turn.
I tried this question, but it only worked from removing entire lists from eachother. Is there a way I could change it to work for this case?
Thanks for your time!
答案1
得分: 0
everyone.remove(enemy_list[num])
英文:
everyone.remove(enemy_list[num])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论