从关键字列表中查找所有句子到字典
我有可能的单词列表来对给定的单词进行变位词。列表中的每一串都是字典的键,并且具有一个或多个单词的值。这是根据字典中相应键的每个列表中的单词按键顺序生成所有可能句子的最佳(最快,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
使用 itertools 中的
product
函数模块生成可迭代的所有组合输出将是元组,但如果需要,可以轻松将它们转换为字符串或列表。
Use the
product
function in the itertools module to produce all combinations of your iterablesThe output will be tuples, but these can easily be converted to strings or lists if required.
这样的东西有用吗?
Does something like this work?
鼓励使用这些产品,我可以弯曲它们以适应列表字典中可变数量的键,如下所示:
Encouraged to monkey with the products I could bend them to adapt variable number of keys from dictionary of lists like this: