Python 根据给定的步骤数生成所有可能的字典组合?

Python 根据给定的步骤数生成所有可能的字典组合?,python,Python,假设: 仅使用4个字母(a、b、c、d) 假设我有一本字典,由4个字母的出现次数(>=0)组成 d = {"a":1, "b":2, "c":1, "d":3} 我得到了一个“步骤”编号 我想找到所有可能的字典,给出一个“步数”的出现减法 比如说 # given the above dictionary and a steps of 2 moo = {"a":1, "b":1, "c":1, "d":2} # moo is a possibility because I simply took

假设:

仅使用4个字母(a、b、c、d)

假设我有一本字典,由4个字母的出现次数(>=0)组成

d = {"a":1, "b":2, "c":1, "d":3}
我得到了一个“步骤”编号

我想找到所有可能的字典,给出一个“步数”的出现减法

比如说

# given the above dictionary and a steps of 2
moo = {"a":1, "b":1, "c":1, "d":2}
# moo is a possibility because I simply took away 1 b and 1 d
# how do I find all the possibilities? (note: occurrences cannot be negative)
编辑:步骤与2个步骤完全相同

注意:我想找到所有的“moo”或所有可能的字典,给出一个参考字典和一些步骤。我不关心测试两个字典是否满足步骤要求

我想我想出了一些递归代码来解决这个问题:

def genDict(d, steps):
    if steps == 0:
        return [d]
    dList = []
    for key, value in d.items():
        if value > 0:
            temp = dict(d)
            temp[key] = value -1
            dList += genDict(temp, steps-1)
    return dList

有没有人有一个不会占用内存的非递归解决方案?

如果我正确理解了你的问题

  • 从字典中获取完整的字符串

    d = {"a":1, "b":2, "c":1, "d":3}
    my_string = ""
    for key, value in d.iteritems():
        my_string += key * value
    # my_string now contains 1 a, 2 b's, 1 c, and 3 d's.
    
  • 用于获取字符串的所有可能排列

    from itertools import permutations
    for i in permutations(my_string):
        print i # Do something meaningful with the output
    

  • 如果我正确理解你的问题

  • 从字典中获取完整的字符串

    d = {"a":1, "b":2, "c":1, "d":3}
    my_string = ""
    for key, value in d.iteritems():
        my_string += key * value
    # my_string now contains 1 a, 2 b's, 1 c, and 3 d's.
    
  • 用于获取字符串的所有可能排列

    from itertools import permutations
    for i in permutations(my_string):
        print i # Do something meaningful with the output
    

  • 它不会占用很多内存,因为它会在递归中更改相同的列表,但是如果您想收集结果而不是只打印结果,则需要在结果列表中附加一个d的deepcopy

    d = map(list, {"a":1, "b":2, "c":1, "d":3}.items())
    step = 2
    def choose(d, pos, step):
        if step == 0:
            print d
            return
        if d[pos][1] > 0:
            d[pos][1] -= 1
            choose(d, pos, step-1)
            d[pos][1] += 1
        if pos < len(d)-1:
            choose(d, pos+1, step)
    choose(d, 0, 2)
    

    它不会占用很多内存,因为它会在递归中更改相同的列表,但是如果您想收集结果而不是只打印结果,则需要在结果列表中附加一个d的deepcopy

    d = map(list, {"a":1, "b":2, "c":1, "d":3}.items())
    step = 2
    def choose(d, pos, step):
        if step == 0:
            print d
            return
        if d[pos][1] > 0:
            d[pos][1] -= 1
            choose(d, pos, step-1)
            d[pos][1] += 1
        if pos < len(d)-1:
            choose(d, pos+1, step)
    choose(d, 0, 2)
    

    整整两个“步骤”还是最多两个“步骤”?@TimPietzcker抱歉,我的意思是整整两个步骤。我建议你阅读Peter Norvig的Python拼写更正器。它包括计算“编辑距离”的代码,也许您可以从中获得一些有用的想法。如果您的字典总是使用单个字母作为键,那么您可以将字典编码为字符串,或者只使用下面的代码!整整两个“步骤”还是最多两个“步骤”?@TimPietzcker抱歉,我的意思是整整两个步骤。我建议你阅读Peter Norvig的Python拼写更正器。它包括计算“编辑距离”的代码,也许您可以从中获得一些有用的想法。如果您的字典总是使用单个字母作为键,那么您可以将字典编码为字符串,或者只使用下面的代码!您还需要包括已删除的字符串<代码>置换(my_字符串,len(my_字符串)-j)用于xrange(步骤)中的j您还需要包括删除的字符串<代码>置换(my_字符串,len(my_字符串)-j)用于xrange中的j(步骤)