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!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top