Python How can we iteratively create sublists from a list based on a sum threshold?

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    List
AI Thread Summary
The discussion focuses on how to iteratively create sublists from a list of numbers based on a specified sum threshold. A PHP function is provided that initializes an empty list and iteratively adds numbers to a sublist until the sum exceeds the threshold, at which point it creates a new sublist. The example uses the numbers [1,2,3,2,4,1] with a threshold of 5, demonstrating how the output is structured into multiple sublists. Participants clarify that the original request was for a Python solution, suggesting that the PHP code can be adapted accordingly. The conversation highlights the importance of specifying programming languages in queries for better assistance.
member 428835
Hello PF!

Initializing with an empty list [], how would we perform the operations [[1]] and then [[1,2]] then [[1,2,3]] then [[1,2,3],[2]] then [[1,2,3], [2,4]] then [[1,2,3],[2,4],[1]] and so on. I'm trying to do this iteratively, so within a for-loop. Basically, given a list of numbers [1,2,3,2,4,1] add them to a sublist until the sum of values of that sublist exceeds a threshold, in which case create a new sublist. I've no clue how to do this, and have googled but to no avail
 
Technology news on Phys.org
You didn't specified a language, but in PHP:

PHP:
<?php
function makeList($numbers, $threshold){
    $list = [];
    $sublist = [];
    $total = 0;

    foreach($numbers as $number){
        $sublist[] = $number;
        $total += $number;
    
        if($total > $threshold){
            $list[] = $sublist;
            $total = 0;
            $sublist = [];       
        }
    }

    if(!empty($sublist)){
        $list[] = $sublist;
    }
    
    return $list;
}

$numbers = [1,2,3,2,4,1];
$threshold = 5;

var_dump(makeList($numbers, $threshold));

Output:

Code:
array(3) {
  [0]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
  }
  [1]=>
  array(2) {
    [0]=>
    int(2)
    [1]=>
    int(4)
  }
  [2]=>
  array(1) {
    [0]=>
    int(1)
  }
}
 
jack action said:
You didn't specified a language
It's in the thread title -- Python.
 
  • Like
Likes jack action
Mark44 said:
It's in the thread title -- Python.
Then it will be left as an exercise for the OP to convert it!
 
jack action said:
Then it will be left as an exercise for the OP to convert it!
I wonder how many teachers have gotten that response from their students.
 
  • Like
Likes jack action
Got it, thanks for the suggestion!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top