【问题标题】:All possible combinations of a word - Python一个单词的所有可能组合 - Python
【发布时间】:2015-05-11 04:00:53
【问题描述】:

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

例如:如果输入是“ABC”,则输出应该是“A”、“B”、“C”、“AB”、“AC”、“BC”、“ABC”、“ACB”等等上...

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

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

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

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

【问题讨论】:

标签: python dictionary random combinations words


【解决方案1】:

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

import itertools

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

列表理解版:

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

【讨论】:

  • 不应该是range(4)吗?甚至 range(1, 4) 没有空的
  • 是的,第二个示例有错字。应该是len(items) + 1,就像第一个例子一样。
  • 在他的例子中他说他想要"ABC","ACB" and so on...。我认为顺序很重要..
  • @Dzarafata - 我假设提问者用“ACB”打了一个小错误,因为在他的评论中他说他不关心订单。
  • 我如果认为他想说他不在乎输出中是否有ABC ACBACB ABC
【解决方案2】:

我喜欢将这样的东西放入一个不错的生成器函数中,例如:

from itertools import permutations

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

然后可以方便地使用,例如:

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

或者因为你想要它作为一个集合:

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

但是,最好生成这个词集,而是检查这些英文单词并检查每个单词是否可以由给定的字母组成。取决于哪个集合更大,这取决于输入的字母。

【讨论】:

  • '' 被视为 'ABC' 的子集。范围可能是0
  • @MartinKonecny 他没有说他想要所有子集,他的列表看起来他想要空词。
【解决方案3】:

可惜我不能对 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)

它进行全长排列。

【讨论】:

    【解决方案4】:

    对于所有小字母组合

    words = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    for a in words:
        print(a)
        for b in words:
            print(a,b)
            for c in words:
                print(a,b,c)
                for d in words:
                    print(a,b,c,d)
                    for e in words:
                        print(a,b,c,d,e)
                        for f in words:
                            print(a,b,c,d,e,f)
                            for g in words:
                                print(a,b,c,d,e,f,g)
                                for h in words:
                                    print(a,b,c,d,e,f,g,h)
    

    【讨论】:

      猜你喜欢
      • 2015-07-26
      • 2022-08-24
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多