【发布时间】:2015-10-16 02:44:37
【问题描述】:
我正在尝试从给定的字符串列表中创建一个长度为 100 个字符的句子。长度必须正好是一百个字符。我们还必须使用排列找到所有可能的句子。每个单词之间必须有空格,并且不允许重复单词。名单如下:
['saintliness', 'wearyingly', 'shampoo', 'headstone', 'dripdry', 'elapse', 'redaction', 'allegiance', 'expressionless', 'awesomeness', 'hearkened', 'aloneness', 'beheld', 'courtship', 'swoops', 'memphis', 'attentional', 'pintsized', 'rustics', 'hermeneutics', 'dismissive', 'delimiting', 'proposes', 'between', 'postilion', 'repress', 'racecourse', 'matures', 'directions', 'bloodline', 'despairing', 'syrian', 'guttering', 'unsung', 'suspends', 'coachmen', 'usurpation', 'convenience', 'portal', 'deferentially', 'tarmacadam', 'underlay', 'lifetime', 'nudeness', 'influences', 'unicyclists', 'endangers', 'unbridled', 'kennedy', 'indian', 'reminiscent', 'ravish', 'republics', 'nucleic', 'acacia', 'redoubled', 'minnows', 'bucklers', 'decays', 'garnered', 'aussies', 'harshen', 'monogram', 'consignments', 'continuum', 'pinion', 'inception', 'immoderate', 'reiterated', 'hipster', 'stridently', 'relinquished', 'microphones', 'righthanders', 'ethereally', 'glutted', 'dandies', 'entangle', 'selfdestructive', 'selfrighteous', 'rudiments', 'spotlessly', 'comradeinarms', 'shoves', 'presidential', 'amusingly', 'schoolboys', 'phlogiston', 'teachable', 'letting', 'remittances', 'armchairs', 'besieged', 'monophthongs', 'mountainside', 'aweless', 'redialling', 'licked', 'shamming', 'eigenstate']
方法:
我的第一种方法是使用回溯和排列来生成所有句子。但我认为复杂性太高了,因为我的列表太大了。
我可以在这里使用任何其他方法或我可以在这里使用的一些内置函数/包吗? python中最好的方法是什么?任何指针都会在这里有所帮助。
【问题讨论】:
-
你可以重复使用一个单词,还是不允许在句子中重复单词?另外,请注意,您可能会关闭此问题,因为您问的是一个广泛的问题。
-
你可能会认为它有两个不同的问题:第一个,找到长度为 100 的单词的每个组合,第二个(更简单的)部分,获取每个组合的每个排列。
-
我认为这是完全可能的。现在正在努力。令人兴奋的问题!
-
如果这是您的核心问题,
all((word in word_list) for word in dynamic_sentence.split()[:-1])+ 更多代码来解释截断位 + 更多代码来确定是否有重复项怎么样?正如 roeland 所提到的,生成(更不用说存储)所有排列是不可行的。 -
for-if 反模式。那你必须改变你的方法。在生成句子时已经应用该约束。将给定句子中的单词一一匹配。然后复杂度从 O(cⁿ) 下降到 O(n)。
标签: python string list permutation