我在python中设置一个简单的句子生成器,以创建尽可能多的单词组合来描述涉及机器人的通用图像集。 (它的故事很长:D)
输出如下内容:' Cyborg Concept Downloadable Illustration'
令人惊讶的是,我写的随机生成只有255个独特的组合。这是脚本:
import numpy
from numpy import matrix
from numpy import linalg
import itertools
from pprint import pprint
import random
m = matrix( [
['Robot','Cyborg','Andoid', 'Bot', 'Droid'],
['Character','Concept','Mechanical Person', 'Artificial Intelligence', 'Mascot'],
['Downloadable','Stock','3d', 'Digital', 'Robotics'],
['Clipart','Illustration','Render', 'Image', 'Graphic'],
])
used = []
i = 0
def make_sentence(m, used):
sentence = []
i = 0
while i <= 3:
word = m[i,random.randrange(0,4)]
sentence.append(word)
i = i+1
return ' '.join(sentence)
def is_used(sentence, used):
if sentence not in used:
return False
else:
return True
sentences = []
i = 0
while i <= 1000:
sentence = make_sentence(m, used)
if(is_used(sentence, used)):
continue
else:
sentences.append(sentence)
print str(i) + ' ' +sentence
used.append(sentence)
i = i+1
使用randint
代替randrange
,我最多可以获得624种组合(即时),然后它会在无限循环中挂起,无法创建更多组合。
我想问题是,是否有更合适的方法来确定矩阵的所有可能组合?
答案 0 :(得分:3)
您可以使用 itertools 来获取矩阵的所有可能组合。我举了一个例子来说明itertools是如何工作的。
import itertools
mx = [
['Robot','Cyborg','Andoid', 'Bot', 'Droid'],
['Character','Concept','Mechanical Person', 'Artificial Intelligence', 'Mascot'],
['Downloadable','Stock','3d', 'Digital', 'Robotics'],
['Clipart','Illustration','Render', 'Image', 'Graphic'],
]
for combination in itertools.product(*mx):
print combination
答案 1 :(得分:0)
您的代码可以使用递归。没有itertools,这是一个策略:
def make_sentences(m, choices = []):
output = []
if len(choices) == 4:
sentence = ""
i = 0
#Go through the four rows of the matrix
#and choose words for the sentence
for j in choices:
sentence += " " + m[i][j]
i += 1
return [sentence] #must be returned as a list
for i in range(0,4):
output += make_sentences(m, choices+[i])
return output #this could be changed to a yield statement
这与您原来的功能完全不同。
选项列表跟踪已选择的m中每个ROW的列索引。当递归方法发现选择了四行时,它会输出一个只有一个句子的列表。
当方法发现选项列表没有四个元素时,它会递归调用自己的四个新选择列表。这些递归调用的结果将添加到输出列表中。