【问题标题】:Getting a list of strings with character removed in permutation获取在排列中删除字符的字符串列表
【发布时间】:2010-12-21 18:54:22
【问题描述】:

我想在排列中从字符串中删除一个字符......

假设我有一个函数

def (string,char):
    # remove char from string

假设我有 aAabbAA 作为字符串和 A 作为字符然后我希望字符串 [aabb,aAabb,aabbA,aabbA, aabbAA,aAabbA ,aAabbA ] 作为输出,即 A 被删除 3 次、2 次、1 次。

我能做到这一点的最佳方法是什么??

非常感谢....

【问题讨论】:

    标签: python string permutation


    【解决方案1】:

    这是一个使用递归的疯狂想法:

    def f(s, c, start):
        i = s.find(c, start)
        if i < 0:
            return [s]
        else:
            return f(s, c, i+1) + f(s[:i]+s[i+1:], c, i)
    
    s = 'aAabbAA'
    print f(s, 'A', 0)
    # ['aAabbAA', 'aAabbA', 'aAabbA', 'aAabb', 'aabbAA', 'aabbA', 'aabbA', 'aabb']
    

    编辑:使用set

    def f(s, c, start):
        i = s.find(c, start)
        if i < 0:
            return set([s])
        else:
            return set.union(f(s, c, i+1), f(s[:i]+s[i+1:], c, i))
    
    s = 'aAabbAA'
    print f(s, 'A', 0)
    # set(['aAabbA', 'aabbAA', 'aAabbAA', 'aabb', 'aAabb', 'aabbA'])
    

    编辑2:使用三元运算符:

    def f(s, c, start):
        i = s.find(c, start)
        return [s] if i < 0 else f(s, c, i+1) + f(s[:i]+s[i+1:], c, i)
    
    s = 'aAabbAA'
    print f(s, 'A', 0)
    # ['aAabbAA', 'aAabbA', 'aAabbA', 'aAabb', 'aabbAA', 'aabbA', 'aabbA', 'aabb']
    

    编辑 3:timeit

    In [32]: timeit.timeit('x = f("aAabbAA", "A", 0)', 
                           'from test3 import f', number=10000) 
    Out[32]: 0.11674594879150391
    
    In [33]: timeit.timeit('x = deperm("aAabbAA", "A")', 
                           'from test4 import deperm', number=10000) 
    Out[33]: 0.35839986801147461
    
    In [34]: timeit.timeit('x = f("aAabbAA"*6, "A", 0)', 
                           'from test3 import f', number=1) 
    Out[34]: 0.45998811721801758
    
    In [35]: timeit.timeit('x = deperm("aAabbAA"*6, "A")', 
                           'from test4 import deperm', number=1) 
    Out[35]: 7.8437530994415283
    

    【讨论】:

    • 您可以使用条件运算符使其更短。
    • 谢谢你,你说的完全正确!见编辑。 (这变成了代码高尔夫。):-)
    • 在不打印的情况下使用timeit,对于更多的迭代和更长的字符串来说,我的似乎更快。见编辑。
    【解决方案2】:

    这是一个可行的解决方案。基本上我使用目标字符和空字符串的所有可能组合的乘积。

    from itertools import product
    
    def deperm(st, c):
        rsts = []
        indexes = [i for i, s in enumerate(st) if s == c]
        for i in product([c, ''], repeat=len(indexes)):
            newst = ''
            for j, ch in enumerate(st):
                if j in indexes:
                    newst += i[indexes.index(j)]
                else:
                    newst += ch
            rsts.append(newst)
        return rsts
    
    for i in deperm('aAabbAA', 'A'):
        print i
    

    这个输出:

    aAabbAA
    aAabbA
    aAabbA
    aAabb
    aabbAA
    aabbA
    aabbA
    aabb
    

    【讨论】:

      【解决方案3】:

      这样的递归算法可能会对您有所帮助。抱歉,我不是 python 冠军,所以你可能需要自己调整语法。伪代码:

      // returns a set of strings (permutations)
      def permutation(string, char)
        if len(string) == 0
          return [] // return empty set
      
        // get the set of permutations of suffix string recursively
       set_of_perm_suffix = permutation(string[1:], char)
      
       // prepend char to every string in set_of_perm
       appended_set = prepend_char(set_of_perm_suffix , string[0])
      
       // if the first char matches the one we should remove, we could either
       // remove it or keep it.
       if (string[0] == char)
         return union_of_sets(set_of_perm_suffix , appended_set)
       else
         // the first char doesn't match the one we should remove,
         // we need to keep it in every string of the set
         return appended_set
      

      【讨论】:

        猜你喜欢
        • 2012-04-18
        • 1970-01-01
        • 2015-10-25
        • 2020-12-14
        • 1970-01-01
        • 1970-01-01
        • 2011-11-01
        • 2021-06-24
        • 2011-04-20
        相关资源
        最近更新 更多