python - 连接多个列表后,我如何知道某个项目属于哪个原始列表?
问题描述
我组合了来自其他不同类型列表的许多列表,当我得到输出时,我如何告诉 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
我可以输出“你撞到一个(插入怪物)”。
解决方案
选择武器后,您可以:
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))
推荐阅读
- algorithm - 非 BST 二叉树中 BST 子树的最大和
- c++ - 如何将现有的 Q_PROPERTY 重新定义为 REQUIRED?
- java - Android FragmentTransaction 只提交一个 Fragment
- azure - 如何开始使用 Microsoft 沉浸式阅读器 SDK?
- android - 当我关闭应用程序时,Android 服务不会重新启动
- c# - 寻找最近的航点
- tensorflow - 无法将 chexnet 预训练的权重文件加载到 Densenet121
- c# - 使用 Websocket .NET 客户端 ping Websocket 服务器
- javascript - 使用函数和变量函数有什么区别和优势?
- angular7 - 如何防止延迟从 Routes 数组以角度加载路径