简体   繁体   English

从给定字符开始的给定长度的字符列表中创建所有可能的单词

[英]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.

相关问题 从给定的单词列表中生成具有“N”长度的所有可能组合(寻找不重复) - Generate all possible combination with “N” length from given words list (Looking for no repeat) 从 python 中的给定单词列表创建所有可能组合的列表 - creating a list of all possible combination from a given list of words in python 从 python 中的给定字符列表生成所有字符串组合 - Generate all string combinations from given character list in python 如何检查列表中的字符是否在给定集中? - How to check if a character from a list is in a given set? 查找不超过给定字符长度的所有字符串组合 - Find all combinations of strings up to given character length 给定一个单词列表,请标识长度为4或更大的所有相同子字符串 - Given a list of words, identify all identical substrings of length 4 or greater Python帮助:给定可选字符生成所有可能的字符串 - Python help: generating all possible strings given optional character 创建所有给定字符集k和lemgth n的字符串 - Create all strings given character set k and lemgth n 在python语言的给定字符串列表中查找所有出现的字符 - Find all the occurrences of a character in a given list of string in python language 如何从另一个元素列表中顺序填充给定长度的列表并递归找到所有可能的组合? - How to sequentially populate a list of given length from another list of elements and recursively find all possible combinations that satisfy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM