示例:
element = ['Flaming', 'Cold']
fire_properties = ['of Fire', 'of Flame']
cold_properties = ['of Ice', 'of Frost']
相当简单。想要制作一个文本随机发生器,并最终得到诸如“火焰之火”的结果,根据这个例子。不幸的是,我不知道如何做到这一点,以便第一个列表的结果将定义第二个结果将从哪个列表。
答案 0 :(得分:5)
这种解决方案怎么样:你构建一个形容词和名词可能组合的地图,然后选择其中一个:
weapon_map = {"Flaming": ["Fire", "Inferno"], "Frozen": ["Cold", "Frost"]}
selection = random.choice(weapon_map.items())
print (selection[0] + " of " + random.choice(selection[1]))
这种方法更易于维护和理解。
答案 1 :(得分:0)
好吧,我第一次写这篇文章,但是鲍里斯更简洁,所以我决定不提交......对于初学者来说可能会更具可读性。
quality_list = ['Flaming', 'Frozen', 'etc.']
quality = random.choice(quality_list)
item_list = ['Sword', 'Dagger', 'Mace', 'etc.']
item = random.choice(item_list)
hot_qualities = ['Flaming', ...]
cold_qualities = ['Frozen', ...]
hot_elements = ['Fire', 'Inferno']
cold_elements = ['Cold', 'Frost']
if quality in hot_qualities:
element = random.choice(hot_elements)
elif quality in cold_qualities:
element = random.choice(cold_elements)
# NB this is Python 2.X's print and string format, adjust accordingly for 3.X
print "%s %s of %s" % (quality, item, element)
虽然在实际项目中我会像鲍里斯那样进一步概括。