Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据的数量分组到某个限制_Python - Fatal编程技术网

Python 将数据的数量分组到某个限制

Python 将数据的数量分组到某个限制,python,Python,如果有人能帮我解决这个问题,我将不胜感激。这里我有一些样品的重量清单 weight_list = [0.2, 0.4, 1.0, 0.7, 0.1, 0.4, 0.9, 0.1, 0.2, 0.4, 0.2, 0.1] 任何样品的最大重量为1kg。我需要将样品重量相加,直到总质量不超过1kg。例如,这是我在基于给定条件求和样本权重后所期望的结果 grouped_list = [0.6, 1.0, 0.8, 0.4, 1.0, 0.9] 这就是我到目前为止所知道的,但它会无限循环 weight

如果有人能帮我解决这个问题,我将不胜感激。这里我有一些样品的重量清单

weight_list = [0.2, 0.4, 1.0, 0.7, 0.1, 0.4, 0.9, 0.1, 0.2, 0.4, 0.2, 0.1]
任何样品的最大重量为1kg。我需要将样品重量相加,直到总质量不超过1kg。例如,这是我在基于给定条件求和样本权重后所期望的结果

grouped_list = [0.6, 1.0, 0.8, 0.4, 1.0, 0.9]
这就是我到目前为止所知道的,但它会无限循环

weight_list = [0.2, 0.4, 1.0, 0.7, 0.1, 0.4, 0.9, 0.1, 0.2, 0.4, 0.2, 0.1]
grouped_list = []
for weight in weight_list:
    total = 0
    while total <= 1:
        total += weight
        if total > 1:
            total -= weight
            grouped_list.append(total)
            total = 0
        else:
            grouped_list.append(total)

我也对全新的方法持开放态度,如果有任何模块或简单的方法可以对其进行分组,请帮助我。

这是有效的,您当前每次都在循环相同的权重,相反,您应该像这样迭代列表:

weight_list = [0.2, 0.4, 1.0, 0.7, 0.1, 0.4, 0.9, 0.1, 0.2, 0.4, 0.2, 0.1]
grouped_list = []
total, count = 0, 0
# increment counter over the weight list
while count < len(weight_list):
    total += weight_list[count]
    if total > 1:
        # add the weight if it recently exceeded 1 by subtracting recent weight
        total -= weight_list[count]
        # round to 1 decimal point (due to floating point math)
        grouped_list.append(round(total, 1))
        total = 0
    else:
        count += 1
# add the left-over total if < 1
if total < 1:
    grouped_list.append(round(total, 1))
print(grouped_list)

这是可行的,您当前每次都在循环相同的权重,相反,您应该像这样迭代列表:

weight_list = [0.2, 0.4, 1.0, 0.7, 0.1, 0.4, 0.9, 0.1, 0.2, 0.4, 0.2, 0.1]
grouped_list = []
total, count = 0, 0
# increment counter over the weight list
while count < len(weight_list):
    total += weight_list[count]
    if total > 1:
        # add the weight if it recently exceeded 1 by subtracting recent weight
        total -= weight_list[count]
        # round to 1 decimal point (due to floating point math)
        grouped_list.append(round(total, 1))
        total = 0
    else:
        count += 1
# add the left-over total if < 1
if total < 1:
    grouped_list.append(round(total, 1))
print(grouped_list)

我想派对晚了一点,但解决这个问题很有趣,所以我的解决方案是使用for循环而不是while:

weight_list = [0.2, 0.4, 1.0, 0.7, 0.1, 0.4, 0.9, 0.1, 0.2, 0.4, 0.2, 0.1]
grouped_list = []

total = 0.0

def main():
    group(len(weight_list), weight_list)
    print(grouped_list)

def group(n, l):
    global total
    for i in range(n):
        total += l[i]
        if total > 1:
            total -= weight_list[i]
            grouped_list.append(round(total, 1))
            total = l[i]
            continue
        if total == 1:
            grouped_list.append(round(total, 1))
            continue
        
main()

我想派对晚了一点,但解决这个问题很有趣,所以我的解决方案是使用for循环而不是while:

weight_list = [0.2, 0.4, 1.0, 0.7, 0.1, 0.4, 0.9, 0.1, 0.2, 0.4, 0.2, 0.1]
grouped_list = []

total = 0.0

def main():
    group(len(weight_list), weight_list)
    print(grouped_list)

def group(n, l):
    global total
    for i in range(n):
        total += l[i]
        if total > 1:
            total -= weight_list[i]
            grouped_list.append(round(total, 1))
            total = l[i]
            continue
        if total == 1:
            grouped_list.append(round(total, 1))
            continue
        
main()

需要合计样品重量,直到总质量不超过1 kg。。如果列表中的每个元素都以KG为单位,那么总和[0.6,1.0,0.8,0.4,1.0,0.9]或1kg以下的总质量是多少。它是4.7公斤。这是你想要的结果吗?您的代码所做的与您在文本中所说的完全不同让我澄清一下,第一个样本的重量是0.2千克,第二个样本的重量是0.4千克。如果我们添加样品1和样品2,总质量为0.6 kg,小于1.0 kg,但如果我们添加第三个样品,则总质量超过1.0 kg,得到1.6 kg。事实上,1.0 kg第三个样品必须保持原样。然后继续进行下去。我们应该使用堆栈进行这种使用。这将使程序易于理解。如果您想知道堆栈版本Hello Ubaid,请告诉我,我确实很想知道。请与我分享您在这方面的知识。需要将样品重量相加,直到总质量不超过1 kg。。如果列表中的每个元素都以KG为单位,那么总和[0.6,1.0,0.8,0.4,1.0,0.9]或1kg以下的总质量是多少。它是4.7公斤。这是你想要的结果吗?您的代码所做的与您在文本中所说的完全不同让我澄清一下,第一个样本的重量是0.2千克,第二个样本的重量是0.4千克。如果我们添加样品1和样品2,总质量为0.6 kg,小于1.0 kg,但如果我们添加第三个样品,则总质量超过1.0 kg,得到1.6 kg。事实上,1.0 kg第三个样品必须保持原样。然后继续进行下去。我们应该使用堆栈进行这种使用。这将使程序易于理解。如果您想知道堆栈版本Hello Ubaid,请告诉我,我确实很想知道。请与我分享你在这方面的知识。