如何在python中进行分区?

问题描述 投票:-2回答:1

将集合划分为两个子集,以使子集和的差为:

minimum(split numbers from A list to B and C so that sum(B) - sum(C) will be minimum among all other options)

位置:

A = [5,6,7,8], B = [5,8], C = [6,7]
python list split set partition
1个回答
0
投票

我想您正在寻找这个?

def partition(S):
    A = []
    B = []
    for i in sorted(S, reverse=True):
        if sum(A) < sum(B):
            A.append(i)
        else:
            B.append(i)
    return (A, B)

S=[5,6,7,8]
print(partition(S))
© www.soinside.com 2019 - 2024. All rights reserved.