0
点赞
收藏
分享

微信扫一扫

Python把列表中的数字尽量等分成n份


问题描述:假设一个列表中含有若干整数,现在要求将其分成n个子列表,并使得各个子列表中的整数之和尽可能接近。

下面的代码并没有使用算法,而是直接将原始列表分成n个子列表,然后再不断地调整各个子列表中的数字,从元素之和最大的子列表中拿出最小的元素放到元素之核最小的子列表中,重复这个过程,知道n个子列表足够接近为止。

import random
def numberSplit(lst, n, threshold):
    '''lst为原始列表,内含若干整数,n为拟分份数
       threshold为各子列表元素之和的最大差值'''
    length = len(lst)
    p = length // n
    #尽量把原来的lst列表中的数字等分成n份
    partitions = []
    for i in range(n-1):
        partitions.append(lst[i*p:i*p+p])
    else:
        partitions.append(lst[i*p+p:])
    print('初始分组结果:', partitions)
    
    #不停地调整各个子列表中的数字
    #直到n个子列表中数字之和尽量相等
    times = 0
    while times < 1000:
        times += 1
        #元素之和最大的子列表和最小的子列表
        maxLst = max(partitions, key=sum)
        minLst = min(partitions, key=sum)
        #把大的子列表中最小的元素调整到小的子列表中
        m = min(maxLst)
        i = [j for j, v in enumerate(maxLst) if v==m][0]
        minLst.insert(0, maxLst.pop(i))
        print('第{0}步处理结果:'.format(times), partitions)
        first = sum(partitions[0])
        for item in partitions[1:]:
            if abs(sum(item)-first) > threshold:
                break
        else:
            break
    else:
        print('很抱歉,我无能为力,只能给出这样一个结果了。')
    return partitions
lst = [random.randint(1, 100) for i in range(10)]
print(lst)
result = numberSplit(lst, 3, 5)
print('最终结果:', result)
#输出各组数字之和
print('各子列表元素之和:')
for item in result:
    print(sum(item))

运行结果一:

Python把列表中的数字尽量等分成n份_python

运行结果二:

Python把列表中的数字尽量等分成n份_java_02

运行结果三:

Python把列表中的数字尽量等分成n份_python_03

举报

相关推荐

0 条评论