从关键字列表中查找所有句子到字典

发布于 2024-09-15 08:27:50 字数 375 浏览 9 评论 0原文

我有可能的单词列表来对给定的单词进行变位词。列表中的每一串都是字典的键,并且具有一个或多个单词的值。这是根据字典中相应键的每个列表中的单词按键顺序生成所有可能句子的最佳(最快,Pythonic)方法。 列表中的键数量可变。

keylist = ['key1', 'key2', 'key3']
worddict = {'key1': ['a','b','c'], 'key2':['d','e','f'], 'key3':['g','h','i']}

预期结果(第一个键列表中的第一个单词,第二个键列表中的第二个单词,依此类推):

["a d g",
"a d h",
"a d i",
.....
"c f i"]

I have list of possible words to make anagram of the given words. Each string of list is key to dictionary and has value of one or more words. Which is the best (fastest, pythonic) way to make all possible sentences in the order of the keys from the words in each list of the corresponding keys in the dictionary.
Lists have variable number of keys in them.

keylist = ['key1', 'key2', 'key3']
worddict = {'key1': ['a','b','c'], 'key2':['d','e','f'], 'key3':['g','h','i']}

Expected result (first word from first keys list, second from second keys list and so on):

["a d g",
"a d h",
"a d i",
.....
"c f i"]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小鸟爱天空丶 2024-09-22 08:27:50

使用 itertools 中的 product 函数模块生成可迭代的所有组合

import itertools

for sentence in itertools.product(['a','b','c'], ['d','e','f'], ['g','h','i']):
    print sentence

输出将是元组,但如果需要,可以轻松将它们转换为字符串或列表。

Use the product function in the itertools module to produce all combinations of your iterables

import itertools

for sentence in itertools.product(['a','b','c'], ['d','e','f'], ['g','h','i']):
    print sentence

The output will be tuples, but these can easily be converted to strings or lists if required.

萧瑟寒风 2024-09-22 08:27:50

这样的东西有用吗?

import itertools
anagrams = []
for x in itertools.product(*worddict.values()):
    anagrams.extend(" ".join(y) for y in itertools.permutations(x))

Does something like this work?

import itertools
anagrams = []
for x in itertools.product(*worddict.values()):
    anagrams.extend(" ".join(y) for y in itertools.permutations(x))
说不完的你爱 2024-09-22 08:27:50

鼓励使用这些产品,我可以弯曲它们以适应列表字典中可变数量的键,如下所示:

import itertools
keylist = ['key1', 'key4','key2']
worddict = {'key1': ['a','b','c'],
            'key2':['d','e','f'],
            'key3':['g','h','i'],
            'key4':['j','k','l']}
sentences = (' '.join(sentence)
             for sentence in itertools.product(*(worddict[k]
                                                 for k in keylist)))
print '\n'.join(sentences)

Encouraged to monkey with the products I could bend them to adapt variable number of keys from dictionary of lists like this:

import itertools
keylist = ['key1', 'key4','key2']
worddict = {'key1': ['a','b','c'],
            'key2':['d','e','f'],
            'key3':['g','h','i'],
            'key4':['j','k','l']}
sentences = (' '.join(sentence)
             for sentence in itertools.product(*(worddict[k]
                                                 for k in keylist)))
print '\n'.join(sentences)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文