首页 > 解决方案 > 连接多个列表后,我如何知道某个项目属于哪个原始列表?

问题描述

我组合了来自其他不同类型列表的许多列表,当我得到输出时,我如何告诉 Python 这是来自组合列表中的哪个列表?

例如,我这里有多个列表。

vampire_weapon = ['stake','silver sword'];
vampire_random = random.randint(0,1)

ghost_weapon = ['flash light','machine'];
ghost_random = random.randint(0,1)

zombie_weapon = ['crossbow','gun'];
zombie_random = random.randint(0,1)

monster_list = ['vampire', 'ghost', 'zombie']
monster_random = random.randint(0,2)

free_move = ['free move foward']

weapon_list = vampire_weapon + ghost_weapon + zombie_weapon
weapon_random = random.randint(0,8)

big_list = weapon_list + monster_list + free_move
big_random = random.randint(0,9)

在底部可以看到我创建了一个名为big_list. 当我运行我的代码时,我会从该代码上方的任何列表中随机选择一个输出。如何对其进行编码,以便 Python 知道这是来自哪个列表,以便我可以像下面的示例一样进行不同的输出?

例如,如果它来自weapon_list,我可以输出“你遇到了(插入武器)”。如果它来自monster_list我可以输出“你撞到一个(插入怪物)”。

标签: python

解决方案


选择武器后,您可以:

weapon = big_list[big_random]

if weapon in vampire_weapon:
    print("You encountered a vampire weapon")
elif weapon in ghost_weapon:
    print("You bumbed into a ghost weapon")
...

而且我建议您根据 的大小来选择不同的随机索引big_list,而不仅仅是硬编码:

big_random = random.randint(0,len(big_list))

推荐阅读