<p>我正在尝试使用python的枕头库生成大约6000个随机图像。现在我不能一次创建超过300个,否则我的程序将达到最大递归深度。我知道这是因为我的递归不是用“n-1”案例调用的。由于所有图像都是使用随机数选择的,我不确定如何解决这个问题。这是我的密码:</p>
<pre><code>## Generate Traits
TOTAL_IMAGES = 300 # Number of random unique images we want to generate
all_images = []
# A recursive function to generate unique image combinations
def create_new_image():
new_image = {}
# For each trait category, select a random trait based on the weightings
new_image ["Plant"] = random.choices(plant, plant_weights)[0]
new_image ["Pot"] = random.choices(pot, pot_weights)[0]
new_image ["Face"] = random.choices(face, face_weights)[0]
if new_image in all_images:
return create_new_image()
else:
return new_image
# Generate the unique combinations based on trait weightings
for i in range(TOTAL_IMAGES):
new_trait_image = create_new_image()
all_images.append(new_trait_image)
</code></pre>