因此,我无法从字典中的随机列表中获取随机项。我现在拥有的代码
tasktotal = ["Medbay/Submit Scan","Medbay/Submit Example","Cafeteria/Connect Wire","Electrical/Connect Wire","Cafeteria/Empty Garbage","Security/Connect Wire","Navigation/Navigate","Upper Engine/Fuel","Lower Engine/Fuel","Shield/Prime Shield","Storage/Empty Garbage","Admin/Swipe Card","Weapon/Clear Asteroid","Admin/Connect Wire","Storage/Connect Wire"]
然后我做了
task = dict(s.split('/') for s in task)
问题是我只能在一个位置执行一项任务。我想在字典中列出它,并从随机位置中选择一个随机任务。
您可以制作一个字典,将键映射到这样的列表(或者,您可以使用defaultDict):
tasktotal = ["Medbay/Submit Scan","Medbay/Submit Example","Cafeteria/Connect Wire","Electrical/Connect Wire","Cafeteria/Empty Garbage","Security/Connect Wire","Navigation/Navigate","Upper Engine/Fuel","Lower Engine/Fuel","Shield/Prime Shield","Storage/Empty Garbage","Admin/Swipe Card","Weapon/Clear Asteroid","Admin/Connect Wire","Storage/Connect Wire"]
d = {}
for k, v in (s.split('/') for s in tasktotal):
d.setdefault(k, []).append(v)
这将使d
如下所示:
{'Medbay': ['Submit Scan', 'Submit Example'],
'Cafeteria': ['Connect Wire', 'Empty Garbage'],
'Electrical': ['Connect Wire'],
'Security': ['Connect Wire'],
'Navigation': ['Navigate'],
'Upper Engine': ['Fuel'],
'Lower Engine': ['Fuel'],
'Shield': ['Prime Shield'],
'Storage': ['Empty Garbage', 'Connect Wire'],
'Admin': ['Swipe Card', 'Connect Wire'],
'Weapon': ['Clear Asteroid']}
鉴于您可以从喜欢的任何列表中获取随机值:
import random
random.choice(d['Medbay'])
# 'Submit Example' (or 'Submit Scan')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句