所有可能的单词组合 - Python

时间:2022-10-29 21:23:46

I am making a program where you insert some letters and then the output are all the posible combinations of that letters.

我正在制作一个程序,你插入一些字母,然后输出是这些字母的所有可能的组合。

For example: if the input is "ABC" the output should be "A","B","C","AB","AC","BC","ABC","ACB" and so on...

例如:如果输入为“ABC”,则输出应为“A”,“B”,“C”,“AB”,“AC”,“BC”,“ABC”,“ACB”等。 。

Finally, my idea is to put all that combinations in a set so that it can be intersect with another set containing a certain dictionary of english words being that intersection the ideal output

最后,我的想法是将所有组合放在一个集合中,以便它可以与另一个集合相交,该集合包含某个英语单词字典,即理想输出的交集

As far, my script is this one:

到目前为止,我的脚本是这样的:

import random

p = list(raw_input('Insert some letters: '))

p2 = []

p3 = []
for j in range((len(p))):
    p2.append(p[j])

for i in range(len(p)):
    a = random.sample(p2,len(p))
    p3.append(str("".join(a)))
    print p3[]

Obviously, there are some errors and its not complete. Can you help me to finish or tell me which path should I take? Thanks for reading

显然,有一些错误而且不完整。你能帮我完成或告诉我应该走哪条路?谢谢阅读

3 个解决方案

#1


If you don't care about order, you are looking for a combination. You can use itertools.combination for this:

如果您不关心订单,那么您正在寻找一种组合。您可以使用itertools.combination:

import itertools

items = 'ABC'
for i in range(len(items)+1):
    for combination in itertools.combinations('ABC', i): 
        print(combination)

List Comprehension version:

列表理解版本:

[combination for i in range(len(items)+1) for combination in itertools.combinations('ABC', i)]

#2


I like putting stuff like this into a nice generator function, for example:

我喜欢把这样的东西放到一个很好的生成器函数中,例如:

from itertools import permutations

def words(letters):
    for n in range(1, len(letters)+1):
        yield from map(''.join, permutations(letters, n))

That can then conveniently be used for example like this:

那么可以方便地使用,例如:

for word in words('ABC'):
    print(word)

Or since you want it as a set:

或者因为你想要它作为一组:

>>> set(words('ABC'))
{'BA', 'BCA', 'CB', 'A', 'AC', 'AB', 'C', 'BC', 'CA', 'ABC', 'CBA', 'B', 'BAC', 'CAB', 'ACB'}

However, it might be a better idea to not generate this word set but go through those English words and check each whether it can be made from the given letters. Depends on which set is larger, which depends on the input letters.

但是,最好不要生成这个单词集,但要仔细阅读这些英语单词并检查每个单词是否可以用给定的字母制作。取决于哪个集更大,这取决于输入字母。

#3


Pity I can not do +1 to Stefan. I have edited Stefan's genius answer(to my purpose):

可惜我不能为Stefan做+1。我编辑了Stefan的天才答案(根据我的目的):

from itertools import permutations

def words(letters):
    yield from map(''.join, permutations(letters, len(letters)))

result = set()
for word in words('algorithm'):
    result.add(word)

print(f' There are {len(result)} combinations')

for i, each in enumerate(result):
    if i%5 == 0 and input(''):
        break
    print(each)

It makes permutations of full length.

它产生全长的排列。

#1


If you don't care about order, you are looking for a combination. You can use itertools.combination for this:

如果您不关心订单,那么您正在寻找一种组合。您可以使用itertools.combination:

import itertools

items = 'ABC'
for i in range(len(items)+1):
    for combination in itertools.combinations('ABC', i): 
        print(combination)

List Comprehension version:

列表理解版本:

[combination for i in range(len(items)+1) for combination in itertools.combinations('ABC', i)]

#2


I like putting stuff like this into a nice generator function, for example:

我喜欢把这样的东西放到一个很好的生成器函数中,例如:

from itertools import permutations

def words(letters):
    for n in range(1, len(letters)+1):
        yield from map(''.join, permutations(letters, n))

That can then conveniently be used for example like this:

那么可以方便地使用,例如:

for word in words('ABC'):
    print(word)

Or since you want it as a set:

或者因为你想要它作为一组:

>>> set(words('ABC'))
{'BA', 'BCA', 'CB', 'A', 'AC', 'AB', 'C', 'BC', 'CA', 'ABC', 'CBA', 'B', 'BAC', 'CAB', 'ACB'}

However, it might be a better idea to not generate this word set but go through those English words and check each whether it can be made from the given letters. Depends on which set is larger, which depends on the input letters.

但是,最好不要生成这个单词集,但要仔细阅读这些英语单词并检查每个单词是否可以用给定的字母制作。取决于哪个集更大,这取决于输入字母。

#3


Pity I can not do +1 to Stefan. I have edited Stefan's genius answer(to my purpose):

可惜我不能为Stefan做+1。我编辑了Stefan的天才答案(根据我的目的):

from itertools import permutations

def words(letters):
    yield from map(''.join, permutations(letters, len(letters)))

result = set()
for word in words('algorithm'):
    result.add(word)

print(f' There are {len(result)} combinations')

for i, each in enumerate(result):
    if i%5 == 0 and input(''):
        break
    print(each)

It makes permutations of full length.

它产生全长的排列。