Python 在条件语句期间,如何从列表中删除元素?

Python 在条件语句期间,如何从列表中删除元素?,python,Python,我正在用Python34(不是pygame)创建一个基于文本的冒险游戏,我有一类角色。然后我把这些角色分成两个列表:善和恶。然后他们之间会有一系列的争斗,但我不知道如果一个角色死了怎么从列表中删除它。战斗是随机的,因此每次都会有不同的角色获胜,这意味着我需要一段代码从列表中删除一个角色,具体取决于谁赢得了战斗。如果您知道要从列表中删除的元素的索引,您可以执行以下操作: yourlist.pop(index_of_the_element_to_be_removed) yourlist.pop(y

我正在用Python34(不是pygame)创建一个基于文本的冒险游戏,我有一类角色。然后我把这些角色分成两个列表:善和恶。然后他们之间会有一系列的争斗,但我不知道如果一个角色死了怎么从列表中删除它。战斗是随机的,因此每次都会有不同的角色获胜,这意味着我需要一段代码从列表中删除一个角色,具体取决于谁赢得了战斗。

如果您知道要从列表中删除的元素的索引,您可以执行以下操作:

yourlist.pop(index_of_the_element_to_be_removed)
yourlist.pop(yourlist.index(value_of_the_element_to_be_removed))
[e for e in yourlist if e!=value_of_the_element_to_be_removed]
如果知道要从列表中删除的元素的值,可以执行以下操作:

yourlist.pop(index_of_the_element_to_be_removed)
yourlist.pop(yourlist.index(value_of_the_element_to_be_removed))
[e for e in yourlist if e!=value_of_the_element_to_be_removed]
如果要删除具有该值的所有元素,可以执行以下操作:

yourlist.pop(index_of_the_element_to_be_removed)
yourlist.pop(yourlist.index(value_of_the_element_to_be_removed))
[e for e in yourlist if e!=value_of_the_element_to_be_removed]

如果知道要从列表中删除的元素的索引,可以执行以下操作:

yourlist.pop(index_of_the_element_to_be_removed)
yourlist.pop(yourlist.index(value_of_the_element_to_be_removed))
[e for e in yourlist if e!=value_of_the_element_to_be_removed]
如果知道要从列表中删除的元素的值,可以执行以下操作:

yourlist.pop(index_of_the_element_to_be_removed)
yourlist.pop(yourlist.index(value_of_the_element_to_be_removed))
[e for e in yourlist if e!=value_of_the_element_to_be_removed]
如果要删除具有该值的所有元素,可以执行以下操作:

yourlist.pop(index_of_the_element_to_be_removed)
yourlist.pop(yourlist.index(value_of_the_element_to_be_removed))
[e for e in yourlist if e!=value_of_the_element_to_be_removed]
如果传递要删除的字符,则可以在好列表和坏列表上调用remove()

good_list.remove('good_guy')

如果传递要删除的字符,则可以在好列表和坏列表上调用remove()

good_list.remove('good_guy')


根据我的理解,我试图模拟每一轮的随机一对一战斗

import random
from __future__ import print_function
characters = ['good1_fox','evil1_elephant','good2_tiger','evil2_lion','good3_bird','evil3_chicken']
# Try to split characters into two lists
good = [x for x in characters if 'good' in x]
evil = [x for x in characters if 'evil' in x]
# Total round of fight
n = 3
for i in xrange(n):
    good_fighter = random.choice(good)
    evil_fighter = random.choice(evil)
    # set the condition of winning
    if len(good_fighter) >= len(evil_fighter):
        # Remove fighter from the list
        evil.remove(evil_fighter)
        print("evil lost {} in fighting with {}".format(evil_fighter, good_fighter))    
    else:
        # Remove fighter from the list          
        good.remove(good_fighter)
        print("good lost {} in fighting with {}".format(good_fighter, evil_fighter))    
print("Remained good fighters: {}\nRemained evil fighters: {}\n".format(", ".join(good),", ".join(evil)))
==打印结果

好老虎在与坏象的战斗中失去了好老虎 邪恶在与善鸟的战斗中失去了邪恶的狮子 好狐狸在与坏象的战斗中失去了好狐狸 优秀战士:好鸟 残存的邪恶战士:邪恶1_大象,邪恶3_鸡

==
这就是你想要的吗?

根据我的理解,我试图模拟每一轮的随机一对一战斗

import random
from __future__ import print_function
characters = ['good1_fox','evil1_elephant','good2_tiger','evil2_lion','good3_bird','evil3_chicken']
# Try to split characters into two lists
good = [x for x in characters if 'good' in x]
evil = [x for x in characters if 'evil' in x]
# Total round of fight
n = 3
for i in xrange(n):
    good_fighter = random.choice(good)
    evil_fighter = random.choice(evil)
    # set the condition of winning
    if len(good_fighter) >= len(evil_fighter):
        # Remove fighter from the list
        evil.remove(evil_fighter)
        print("evil lost {} in fighting with {}".format(evil_fighter, good_fighter))    
    else:
        # Remove fighter from the list          
        good.remove(good_fighter)
        print("good lost {} in fighting with {}".format(good_fighter, evil_fighter))    
print("Remained good fighters: {}\nRemained evil fighters: {}\n".format(", ".join(good),", ".join(evil)))
==打印结果

好老虎在与坏象的战斗中失去了好老虎 邪恶在与善鸟的战斗中失去了邪恶的狮子 好狐狸在与坏象的战斗中失去了好狐狸 优秀战士:好鸟 残存的邪恶战士:邪恶1_大象,邪恶3_鸡

==
这就是你想要的吗?

作为Allen mentioend,你也可以使用list。pop()作为Allen mentioend,你也可以使用list。pop()注意,第二行代码将只删除字符串中要删除的元素的第一个
值。注意,第二行代码将只删除字符串中要删除的元素的第一个
值。