首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python迭代工具排列,通过索引比较缩小结果,而不是预期的工作。

Python迭代工具排列,通过索引比较缩小结果,而不是预期的工作。
EN

Stack Overflow用户
提问于 2013-12-15 08:13:07
回答 2查看 226关注 0票数 0

这里的python试图找出为什么我的代码没有给出预期的结果。第一,守则:

代码语言:javascript
复制
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在其中扮演它的角色)

对此有任何帮助,表示感谢。

达伦

编辑:这是为我打印出来的内容的示例:

代码语言:javascript
复制
[('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'), 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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种不同的排列:

代码语言:javascript
复制
>>> len(list(permutations([','] * 3 + ['and'])))
24

换句话说,对于任何给定的名词排列,你都会在不同的位置用三个逗号和单词and产生24个变体。

只产生名词的120个组合:

代码语言:javascript
复制
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])
票数 1
EN

Stack Overflow用户

发布于 2013-12-15 09:05:50

如果复制不重要,你可以只使用itertools.product

代码语言:javascript
复制
for words in itertools.product(*(['a'], ['big', 'fat'], ['dog', 'house'])):
    print(' '.join(words))

其中的指纹:

代码语言:javascript
复制
a big dog
a big house
a fat dog
a fat house

但既然他们这么做了,你就得做一些更复杂的事情:

代码语言:javascript
复制
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)

从本质上说,它创造了所有可能的顺序词的正确位置,循环通过所有部分的词性,并“叠”在一起的句子。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20592526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档