Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在使用random时使用将子列表与其列表相匹配?_Python_Python 3.x - Fatal编程技术网

Python 如何在使用random时使用将子列表与其列表相匹配?

Python 如何在使用random时使用将子列表与其列表相匹配?,python,python-3.x,Python,Python 3.x,我可能会解释这很奇怪,我为此道歉。我和我的朋友正在创建一个D&D随机化器,每当主事件随机化时,该主事件的子事件也会随机化,但子列表必须与其配对的主事件匹配 例如,CaravanMain事件和Sub-Event监狱交通应该成对进行。穿黑色斗篷的人在“主要事件”和“非敌对文化”SSUB事件中应该是成对的。但当我运行随机发生器时,有时它会把穿着黑色斗篷的人和监狱交通工具一起滚动,而这两个人本不应该配对。这是我的全部代码。无论谁花时间帮助我,你都是最好的。新的堆栈溢出 您只需从随机事件中选择一个随机事件

我可能会解释这很奇怪,我为此道歉。我和我的朋友正在创建一个D&D随机化器,每当主事件随机化时,该主事件的子事件也会随机化,但子列表必须与其配对的主事件匹配

例如,CaravanMain事件和Sub-Event监狱交通应该成对进行。穿黑色斗篷的人在“主要事件”和“非敌对文化”SSUB事件中应该是成对的。但当我运行随机发生器时,有时它会把穿着黑色斗篷的人和监狱交通工具一起滚动,而这两个人本不应该配对。这是我的全部代码。无论谁花时间帮助我,你都是最好的。新的堆栈溢出


您只需从随机事件中选择一个随机事件一次。目前,您正在选择一个随机事件来确定if random.choiceRandom_Events==re1:行的子列表,并选择一个单独的随机事件来实际打印printrandom.choiceRandom_Events行。此外,使用dict将事件映射到子列表可以简化事情

遵循这些指导原则,您应该得到如下结果:

event_dict = {
    re1 : [re1_sub1, re1_sub2, re1_sub3, re1_sub4, re1_sub5, re1_sub6],
    re2 : [re2_sub1, re2_sub2, re2_sub3, re2_sub4, re2_sub5, re2_sub6]
}

# Only select from Random_Events once, storing its value so we can use it
# both to determine the sublist and to print it out
random_event = random.choice(Random_Events)
sublist = event_dict[random_event]
random_sublist_item = random.choice(sublist)

print(random_sublist_item)
print(random_event)

你期望某个事件与另一个预定发生的事件配对。 因此不可能是随机的

如果要将这两个事件配对,请按如下所示更改代码

if random.choice(Random_Events) == re1:
    print(re1_sublist[1])#extract the string from the list
else:
    print(re1_sublist[2])

print(random.choice(Random_Events))
if random.choice(Random_Events) == re1:
    print(re1_sublist[1])#extract the string from the list
else:
    print(re1_sublist[2])

print(random.choice(Random_Events))