这里的python试图找出为什么我的代码没有给出预期的结果。第一,守则:
from itertools import permutations
word_list = ['eggs', ',', 'bacon', ',', 'chicken', ',', 'cheese', 'and', 'tomatoes']
grammar_list = ['NOUN', ',', 'NOUN', ',', 'NOUN', ',', 'NOUN', 'AND', 'NOUN']
def permute_nouns():
permuted_list = []
comma_AND_indices = [index for index, p in enumerate(grammar_list) if p == "," or p == "AND"]
# so 'comma_AND_indices' = [1, 3, 5, 7]
for perm in permutations(word_list):
observed_comma_AND_indices = [index for index, p in enumerate(perm) if p == "," or p == "and"]
if comma_AND_indices == observed_comma_AND_indices:
# what goes wrong here? not matches from list compare above still get appended below.
permuted_list.append(perm)
print permuted_list
permute_nouns()在这个函数中,我使用迭代工具排列方法来创建word_list的排列。然而,我不想要所有的排列。我只想让逗号和单词‘和’保持它们在word_list中的原始位置/索引的排列,并将它们附加到permuted_list中。
我正在使用代码行if comma_AND_indices == observed_comma_AND_indices:来筛选出我不想要的那些排列,但是它不起作用,我也不明白为什么。在打印permuted_list时,我发现逗号和‘和’没有保留,但是所有的排列都附加在后面。
(您可能想知道为什么要在函数中使用grammar_list,但是这里的代码是一个稍微大一点的脚本的一部分,grammar_list在其中扮演它的角色)
对此有任何帮助,表示感谢。
达伦
编辑:这是为我打印出来的内容的示例:
[('eggs', ',', 'bacon', ',', 'chicken', ',', 'cheese', 'and', 'tomatoes'), ('eggs', ',', 'bacon', ',', 'chicken', ',', 'tomatoes', 'and', 'cheese'), ('eggs', ',', 'bacon', ',', 'chicken', 'and', 'cheese', ',', 'tomatoes'), ('eggs', ',', 'bacon', ',', 'chicken', 'and', 'tomatoes', ',', 'cheese'), ('eggs', ',', 'bacon', ',', 'cheese', ',', 'chicken', 'and', 'tomatoes'), ('eggs', ',', 'bacon', ',', 'cheese', ',', 'tomatoes', 'and', 'chicken'), ('eggs', ',', 'bacon', ',', 'cheese', 'and', 'chicken', ',', 'tomatoes'), ('eggs', ',', 'bacon', ',', 'cheese', 'and', 'tomatoes', ',', 'chicken'), ('eggs', ',', 'bacon', ',', 'tomatoes', ',', 'chicken', 'and', 'cheese'), ('eggs', ',', 'bacon', ',', 'tomatoes', ',', 'cheese', 'and', 'chicken'), ('eggs', ',', 'bacon', ',', 'tomatoes', 'and', 'chicken', ',', 'cheese'), ('eggs', ',', 'bacon', ',', 'tomatoes', 'and', 'cheese', ',', 'chicken'), ('eggs', ',', 'bacon', ',', 'chicken', ',', 'cheese', 'and', 'tomatoes'), ('eggs', ',', 'bacon', ',', 'chicken', ',', 'tomatoes', 'and', 'cheese'), ('eggs', ',', 'bacon', ',', 'chicken', 'and', 'cheese', ',', 'tomatoes'), ('eggs', ',', 'bacon', ',', 'chicken', 'and', 'tomatoes', ',', 'cheese'), ('eggs', ',', 'bacon', ',', 'cheese', ',', 'chicken', 'and', 'tomatoes'), ('eggs', ',', 'bacon', ',', 'cheese', ',', 'tomatoes', 'and', 'chicken'), ('eggs', ',', 'bacon', ',', 'cheese', 'and', 'chicken', ',', 'tomatoes'), ('eggs', ',', 'bacon', ',', 'cheese', 'and', 'tomatoes', ',', 'chicken'), ('eggs', ',', 'bacon', ',', 'tomatoes', ',', 'chicken', 'and', 'cheese'), ('eggs', ',', 'bacon', ',', 'tomatoes', ',', 'cheese', 'and', 'chicken'), ('eggs', ',', 'bacon', ',', 'tomatoes', 'and', 'chicken', ',', 'cheese'), ('eggs', ',', 'bacon', ',', 'tomatoes', 'and', 'cheese', ',', 'chicken'), ('eggs', ',', 'bacon', 'and', 'chicken', ',', 'cheese', ',', 'tomatoes'), ('eggs', ',', 'bacon', 'and', 'chicken', ',', 'tomatoes', ',', 'cheese'), ('eggs', ',', 'bacon', 'and', 'chicken', ',', 'cheese', ',', 'tomatoes'), ('eggs', ',', 'bacon', 'and', 'chicken', ',', 'tomatoes', ',', 'cheese'), ('eggs', ',', 'bacon', 'and', 'cheese', ',', 'chicken', ',', 'tomatoes'), ('eggs', ',', 'bacon', 'and', 'cheese', ',', 'tomatoes', ',', 'chicken'), ('eggs', ',', 'bacon', 'and', 'cheese', ',', 'chicken', ',', 'tomatoes'), ('eggs', ',', 'bacon', 'and', 'cheese', ',', 'tomatoes', ',', 'chicken'), ('eggs', ',', 'bacon', 'and', 'tomatoes', ',', 'chicken', ',', 'cheese'), ('eggs', ',', 'bacon', 'and', 'tomatoes', ',', 'cheese', ',', 'chicken'), ('eggs', ',', 'bacon', 'and', 'tomatoes', ',', 'chicken', ',', 'cheese'), ('eggs', ',', 'bacon', 'and', 'tomatoes', ',', 'cheese', ',', 'chicken'), ('eggs', ',', 'chicken', ',', 'bacon', ',', 'cheese', 'and', 'tomatoes'), ('eggs', ',', 'chicken', ',', 'bacon', ',', 'tomatoes', 'and', 'cheese'), ('eggs', ',', 'chicken', ',', 'bacon', 'and', 'cheese', ',', 'tomatoes'), ('eggs', ',', 'chicken', ',', 'bacon', 'and', 'tomatoes', ',', 'cheese'), ('eggs', ',', 'chicken', ',', 'cheese', ',', 'bacon', 'and', 'tomatoes'), ('eggs', ',', 'chicken', ',', 'cheese', ',', 'tomatoes', 'and', 'bacon'), ('eggs', ',', 'chicken', ',', 'cheese', 'and', 'bacon', ',', 'tomatoes'), ('eggs', ',', 'chicken', ',', 'cheese', 'and', 'tomatoes', ',', 'bacon'), ('eggs', ',', 'chicken', ',', 'tomatoes', ',', 'bacon', 'and', 'cheese'), ('eggs', ',', 'chicken', ',', 'tomatoes', ',', 'cheese', 'and', 'bacon'), ('eggs', ',', 'chicken', ',', 'tomatoes', 'and', 'bacon', ',', 'cheese'), ('eggs', ',', 'chicken', ',', 'tomatoes', 'and', 'cheese', ',', 'bacon'), ('eggs', ',', 'chicken', ',', 'bacon', ',', 'cheese', 'and', 'tomatoes'), ('eggs', ',', 'chicken', ',', 'bacon', ',', 'tomatoes', 'and', 'cheese'), ('eggs', ',', 'chicken', ',', 'bacon', 'and', 'cheese', ',', 'tomatoes'), ('eggs', ',', 'chicken', ',', 'bacon', 'and', 'tomatoes', ',', 'cheese'), ('eggs', ',', 'chicken', ',', 'cheese', ',', 'bacon', 'and', 'tomatoes'), ('eggs', ',', 'chicken', ',', 'cheese', ',', 'tomatoes', 'and', 'bacon'), ('eggs', ',', 'chicken', ',', 'cheese', 'and', 'bacon', ',', 'tomatoes'), 发布于 2013-12-15 08:33:25
您的代码工作得很好,尽管您可以更快地生成相同的列表--这里使用product()表示[','] + 3 + ['and']和[w for w in word_list if w not in (',', 'and')]的排列,从而产生相同的120 * 24 = 2880组合。
如果您只期望得到120个结果,那么您就会忘记,您没有测试输出中的3个逗号和单词'and'的顺序;允许对该列表进行24种不同的排列:
>>> len(list(permutations([','] * 3 + ['and'])))
24换句话说,对于任何给定的名词排列,你都会在不同的位置用三个逗号和单词and产生24个变体。
只产生名词的120个组合:
nouns = [w for w in word_list if w not in (',', 'and')]
grammar = [w for w in word_list if w in (',', 'and')]
result = []
for perm in permutations(nouns):
result.append([w for word, g in map(None, perm, grammar) for w in (word, g) if w is not None])发布于 2013-12-15 09:05:50
如果复制不重要,你可以只使用itertools.product
for words in itertools.product(*(['a'], ['big', 'fat'], ['dog', 'house'])):
print(' '.join(words))其中的指纹:
a big dog
a big house
a fat dog
a fat house但既然他们这么做了,你就得做一些更复杂的事情:
import itertools
import collections
grammar = ['NOUN', ',', 'NOUN', ',', 'NOUN', ',', 'NOUN', 'AND', 'NOUN']
parts_of_speech = {
'NOUN': ['eggs', 'bacon', 'chicken', 'cheese', 'tomatoes'],
'AND': ['and'],
',': [',']
}
def partial_sentences(words, indices, sentence_length):
if len(indices) > len(words):
orderings = itertools.product(words, repeat=len(indices))
else:
orderings = itertools.permutations(words, len(indices))
for words in orderings:
sentence = [None] * sentence_length
for index, word in zip(indices, words):
sentence[index] = word
yield sentence
def pos_stacks(parts_of_speech, grammar):
positions = collections.defaultdict(list)
for index, pos in enumerate(grammar):
positions[pos].append(index)
for pos, indices in positions.items():
yield partial_sentences(parts_of_speech[pos], indices, len(grammar))
for result in itertools.product(*pos_stacks(parts_of_speech, grammar)):
sentence = [next(itertools.ifilter(bool, words)) for words in zip(*result)]
print(sentence)从本质上说,它创造了所有可能的顺序词的正确位置,循环通过所有部分的词性,并“叠”在一起的句子。
https://stackoverflow.com/questions/20592526
复制相似问题