【问题标题】:Generate Unique Combination from List of Bills Summing to specific Value从汇总到特定值的票据列表生成唯一组合
【发布时间】:2022-08-22 20:37:29
【问题描述】:

我需要一些逻辑或想法来实现以下用例:

基本输入列表:

data=[
[\"10000025\",710],
[\"1000138833\",1065],
[\"100274005\",820],
[\"1353180\",3160],
[\"481584\",3670],
[\"4851845845\",1690],
[\"485584\",1310],
[\"48848448\",1000],
[\"49849948\",1050],
[\"585598\",4620],
[\"84154858584\",620],
[\"841584584\",2050],
[\"8451184584\",2860],
[\"845188956\",1800],
[\"845845184\",1300],
[\"8458484\",2300],
[\"8954884\",1780],
[\"9416481584\",2720],
[\"9448155584\",1000],
[\"94949494\",1000],
[\"959494158\",1590],
[\"98558858\",1550]
]

预期输出: 总和为 10000 的列表中的组合列表,如果没有特定值的总和,则每个组合最多包含 6 个元素的剩余元素列表。

我尝试关注一些线程:

Find all combinations of a list of numbers with a given sum

How to get all combination for a given sum with a given number of elements

我的代码:

data = [
    [\"10000025\", 710], [\"1000138833\", 1065], [\"100274005\", 820], [\"1353180\", 3160], [\"481584\", 3670],
    [\"4851845845\", 1690], [\"485584\", 1310], [\"48848448\", 1000],
    [\"49849948\", 1050], [\"585598\", 4620], [\"84154858584\", 620], [\"841584584\", 2050], [\"8451184584\", 2860],
    [\"845188956\", 1800], [\"845845184\", 1300], [\"8458484\", 2300], [\"8954884\", 1780], [\"9416481584\", 2720],
    [\"9448155584\", 1000],
    [\"94949494\", 1000],
    [\"959494158\", 1590], [\"98558858\", 1550]
]
valueList = [x[1] for x in data]


def GetNumbers(number):
    result = []
    for i in sorted(valueList, reverse=True):
        sum_list = sum(result)
        if sum_list + i == number:
            result.append(i)
            return result
        elif sum_list + i < number:
            result.append(i)
    return result


for i in range(0, len(data)):
    print(GetNumbers(10000))

代码中的输出可以在列表或字典的列表中,以可实现的为准。

    标签: python


    【解决方案1】:

    这是一个解决此问题的代码,但是复杂性很高,因此如果数据集中有更多元素,计算时间很快就会变得太长。

    data=[
    ["10000025",710],
    ["1000138833",1065],
    ["100274005",820],
    ["1353180",3160],
    ["481584",3670],
    ["4851845845",1690],
    ["485584",1310],
    ["48848448",1000],
    ["49849948",1050],
    ["585598",4620],
    ["84154858584",620],
    ["841584584",2050],
    ["8451184584",2860],
    ["845188956",1800],
    ["845845184",1300],
    ["8458484",2300],
    ["8954884",1780],
    ["9416481584",2720],
    ["9448155584",1000],
    ["94949494",1000],
    ["959494158",1590],
    ["98558858",1550]
    ]
    
    
    targetAmmount = 10000
    groupMaxSize = 6
    
    
    def createGroup(current, remaining_data):
        currentAmmount = sum(e[1] for e in current)
        missingAmmount = targetAmmount - currentAmmount
    
        if len(current) >= groupMaxSize:
            if currentAmmount == targetAmmount:
                yield current
            else:
                yield None
        
    
        possibilities = sorted(filter(lambda e: e[1] <= missingAmmount, remaining_data), key=lambda e: e[1], reverse=True)
        for possibility in possibilities:
            next_current = [*current, possibility]
            next_remaining = list(filter(lambda e: e!=possibility, possibilities))
    
    
            if sum(e[1] for e in next_current) == targetAmmount:
                yield next_current
            else:
                for res in createGroup(next_current, next_remaining):
                    yield res
    
    
    
    res = []
    ungrouped = []
    
    while len(data) != 0:
        first = [data.pop(0)]
    
        groupeCreated = False
        for group in createGroup(first, data):
            if group != None:
                groupeCreated = True
                res.append(group)
                for elem in group:
                    if elem in data:
                        data.remove(elem)
                break
    
        if not groupeCreated:
            ungrouped.append(first[0])
    
    
    print("Res : \n", res)
    print("Ungrouped : \n", ungrouped)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多