从Python中使用另一个列表的占位符来移除列表中的元素。

huangapple go评论80阅读模式
英文:

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])

huangapple
  • 本文由 发表于 2020年1月3日 22:43:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定