[英]Create all the possible words from a list of character for a given length that starts with a given character
I need to create all the possible word list from a list of characters for a given length that starts with a given character. 我需要从以给定字符开始的给定长度的字符列表中创建所有可能的单词列表。
Eg: 例如:
char_list = a,b,c
min_len = 2
max_len = 3
Update: 更新:
start with = c
so the fucntion should return something like this: 所以功能应该返回如下内容:
ca, cb, cc, cab, cac, cba, cbc...
How would I do this in python? 我将如何在python中做到这一点? Your help will be very much appreciated. 非常感谢您的帮助。 Thank you. 谢谢。
You create a product object using the itertools.product
class from the standard library 您可以使用标准库中的itertools.product
类创建产品对象
import itertools
[''.join(i) for i in itertools.product(char_list, repeat= 2) if i[0].startswith('c')]
['ca', 'cb', 'cc']
[''.join(i) for i in itertools.product(char_list, repeat=3) if i[0].startswith('c')]
['caa', 'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']
To generate a word list for range of numbers use a generator function
要生成数字范围的单词列表,请使用generator function
def generate_word(chars_list, my_char, min_len, max_len):
for i in range(min_len, max_len+1):
for j in itertools.product(chars_list, repeat=i):
if j[0].startswith(my_char):
yield ''.join(j)
for word in generate_word(char_list, 'c', 2, 4):
print(word)
Output 输出量
ca
cb
cc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc
caaa
caab
caac
caba
cabb
cabc
caca
cacb
cacc
cbaa
cbab
....
Here's a program that uses a similar technique to Eithos's code. 这是一个使用与Eithos代码类似的技术的程序。 It creates a generator so you can get the words one at a time, if you want. 它创建了一个生成器,因此您可以一次获取一个单词。 The generator code will work on Python 2.6, and later, but you'll need to change the print
for Python 3. 生成器代码将在Python 2.6和更高版本上运行,但是您需要更改Python 3的print
格式。
#!/usr/bin/env python
import itertools
def word_generator(chars, start_with, min_len, max_len):
for i in range(min_len - 1, max_len):
for s in itertools.product(chars, repeat=i):
yield start_with + ''.join(s)
for word in word_generator('abc', 'c', 2, 4):
print word
output 输出
ca
cb
cc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc
caaa
caab
caac
caba
cabb
cabc
caca
cacb
cacc
cbaa
cbab
cbac
cbba
cbbb
cbbc
cbca
cbcb
cbcc
ccaa
ccab
ccac
ccba
ccbb
ccbc
ccca
cccb
cccc
If you want the words in a list rather than one at a time, you can do: 如果您希望列表中的单词而不是一次,则可以执行以下操作:
word_list = list(word_generator('abc', 'c', 2, 4))
This will do exactly what you want, I think: 我认为这将完全满足您的要求:
I made a mistake earlier when I thought itertools.combinations_with_replacement
would do the trick. 当我以为itertools.combinations_with_replacement
可以解决问题时,我就犯了一个错误。 Michael's post (which originally used permutations
) made me reflect on my choice, so I started re-working the code. Michael的帖子(最初使用permutations
)使我思考了自己的选择,因此我开始重新编写代码。 I actually got it working, but I quickly realized it was not the right tool for the job. 我实际上使它工作了,但是我很快意识到这不是完成任务的正确工具。 Then I realized what you really needed is itertools.product
然后我意识到您真正需要的是itertools.product
from itertools import product
minLen = 4
maxLen = 8
sChar = 'a'
print [sChar + ''.join(i) for x in range(minLen-1, maxLen)
for i in product(['a','b','c'], repeat=x)]
But if you try a smaller input to test: 但是,如果您尝试使用较小的输入进行测试:
minLen = 2
maxLen = 3
you get: 你得到:
['aa', 'ab', 'ac', 'aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc']
We can also use c
(per the updated post): 我们还可以使用c
(根据更新的帖子):
minLen = 2
maxLen = 3
sChar = 'c'
['ca', 'cb', 'cc', 'caa', 'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.