Python itertools排列通过索引比较缩小了结果范围,没有按预期工作
有些python新手试图弄明白为什么我的代码没有给出预期的结果。首先是守则:Python itertools排列通过索引比较缩小了结果范围,没有按预期工作,python,permutation,list-comprehension,itertools,Python,Permutation,List Comprehension,Itertools,有些python新手试图弄明白为什么我的代码没有给出预期的结果。首先是守则: from itertools import permutations word_list = ['eggs', ',', 'bacon', ',', 'chicken', ',', 'cheese', 'and', 'tomatoes'] grammar_list = ['NOUN', ',', 'NOUN', ',', 'NOUN', ',', 'NOUN', 'AND', 'NOUN'] def permute
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()
在这个函数中,我使用itertools排列方法创建单词列表的排列
。然而,我不想要所有的排列。我只想要逗号和单词“and”在单词列表中保持其原始位置/索引的排列,并将它们附加到排列列表中
我使用代码行如果逗号和_索引==观察到的_逗号和_索引:
来过滤掉我不想要的那些排列,但它不起作用,我不明白为什么。在打印出排列列表时
我发现逗号和“and”没有被保留,但所有排列都被追加
(您可能想知道为什么要在函数中使用grammar\u list
,但是这里的代码是一个稍大的脚本的一部分,在这个脚本中grammar\u 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'),
您的代码运行得很好,尽管您可以更快地生成相同的列表,使用[',']+3+['和']
和[w在word_列表中,如果w不在(','和')]
中,则更简洁地生成相同的列表,生成相同的120*24=2880组合
如果您只希望得到120个结果,那么您忘记了您没有测试输出中3个逗号和单词”和“
的顺序;该列表允许有24种不同的排列:
>>> len(list(permutations([','] * 3 + ['and'])))
24
换句话说,对于任何给定的仅仅是名词的排列,你产生了24种句子变体,其中有3个逗号,单词和位于不同的位置
要生成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])
如果副本不重要,您可以使用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)
基本上,它在适当的位置创建单词的所有可能顺序,在所有词类中循环,并将句子“堆叠”在一起。为什么不去掉逗号和和
s。这样会更有效率。我无法重现你的问题;我看到2880个版本符合您的标准,而总排列为362880个。您介意发布预期输出的一部分吗?我已经添加了部分打印输出,您将看到“和”改变了位置。这就是说,我现在注意到逗号保持着它们的位置,除非逗号和大便指向它们应该在的位置。