打印字符串的powerset

时间:2012-09-28 01:31:46

标签: python powerset

我正在尝试编写python代码来打印字符串的powerset,但是遇到了一些错误。这就是我所拥有的:

def getperm (string):
    perm = []
    if len(string) == 0:
        perm.append("")
        return perm
    #if len(string) == 1:
    #   perm.append(string)
    #   perm.append("")
    first = string[0]
    print "first = " + str(first)
    rem = string[1:len(string)]
    print "rem = " + str(rem)
    words = getperm(rem)
    for word in words:
        for i in range(len(word)):
            temp = string[0:i] + first + string[i:len(string)]
            print "temp = " + str(temp)
            perm.append(temp)

    return perm

if __name__=="__main__":
    a = "ab"
    mag  = getperm(a)
    print mag

我的预期输出是:

['', 'a', 'b', 'ab']

我的实际输出是:

[]

任何人都可以帮我弄清楚发生了什么事吗?这是python的一些细微差别,还是我的代码中有错误?我认为我的代码应该没问题 - 我将离开第五版Cracking the coding采访

谢谢!

6 个答案:

答案 0 :(得分:4)

你过分思考

这部分试图做太多

for word in words:
    for i in range(len(word)):
        temp = string[0:i] + first + string[i:len(string)]
        print "temp = " + str(temp)
        perm.append(temp)

看看它应该是多么简单

def get_powerset (string):
    perm = []
    if len(string) == 0:
        perm.append("")
        return perm
    #if len(string) == 1:
    #   perm.append(string)
    #   perm.append("")
    first = string[0]
    print "first = " + str(first)
    rem = string[1:len(string)]
    print "rem = " + str(rem)
    words = get_powerset(rem)
    perm.extend(words)
    for word in words:
        perm.append(first+word)

    return perm

if __name__=="__main__":
    a = "ab"
    mag  = get_powerset(a)
    print mag

现在你应该能够通过一点点重构使代码看起来更好

答案 1 :(得分:3)

这是你想要的吗?

import itertools as it

def func(s):
    for i in range(len(s)+1):
        for combo in it.combinations(s,i):
            yield "".join(combo)

print list(func("abc"))

答案 2 :(得分:1)

有一种排列方法:

>>> import itertools
>>> chars = "ABCD"
>>> perms = list(itertools.permutations(chars))
>>> print(perms)
[('A', 'B', 'C'),
 ('A', 'C', 'B'),
 ('B', 'A', 'C'),
 ('B', 'C', 'A'),
 ('C', 'A', 'B'),
 ('C', 'B', 'A')]

答案 3 :(得分:1)

这是重构的迭代解决方案,没有 n = length(xsignal); dt = 1/fs; xi = [(0 : (n-1)) ]* ((2 * pi)/(n*dt)); f0 = 1; omega0 = 2*pi*f0; Ffactor = 4*pi/(omega0 + sqrt(2+omega0^2)); Freq = (xi*Ffactor)./(2*pi); 模块:

itertools

答案 4 :(得分:0)

您是否尝试过跟踪算法实际执行的操作?

getperm('ab'):
  first, rem = 'a', 'b'
  words = getperm('b')
    first, rem = 'b', ''
    words = getperm('')
    words = ['']
    for word in words:
      for i in range(len(word)):
        pass # only called on '', so doesn't matter
    return []
  words = []
  for word in words:
    pass # only called on [], so doesn't matter

所以,这里没有Python的细微差别;您的算法以O(N)步骤返回空列表,并且您已在Python中正确编码该算法。

(当然,您可以添加一些更有用的打印语句,看看每个步骤实际上在做什么,而不是手工跟踪。)

它可能不是您想要的算法,但您需要告诉我们您尝试要做什么。你是否,例如,将一些来自Hoare的伪代码移植到Python中?如果是这样,伪代码是什么?

答案 5 :(得分:0)

使用powerset中的more_itertools

>>> import more_itertools

>>> ["".join(p) for p in list(more_itertools.powerset("ab"))]
['', 'a', 'b', 'ab']

powerset是直接从itertools recipes实施的便捷功能。