2

I want a subset list from input List based on input integer value.

For Example:

Input List: [3,7,9,11,12]
Input Value: 2
Output List: [1,7,9,11,12]
# 2 is subtracted from first element of list

Input List: [3,7,9,11,12]
Input Value: 5
Output List: [5,9,11,12]
#5 is subtracted from list in sequence manner, first 3 is subtracted then remaining 2 is subtracted from 7 hence output is [5,9,11,12]
4
  • Where is the code you used to try and accomplish this? Commented Jan 9, 2020 at 13:21
  • m = 2 sum = 0 for i in range(len(temp)): sum = sum + temp[i] if temp[0] == m: del temp[0] break elif m < temp[i]: temp[i] = temp[i] - m break elif m == sum: temp = temp[i:] break Commented Jan 9, 2020 at 13:22
  • Please explain your second example. Commented Jan 9, 2020 at 13:27
  • Please check I have updated Commented Jan 9, 2020 at 13:30

3 Answers 3

2

Use numpy.cumsum() if modules are allowed:

import numpy as np
input_list = np.asarray([3, 7, 9, 11, 12])
input_integer = 5
output_list = input_list[input_list.cumsum() > input_integer]
output_list[0] -= input_integer - input_list[input_list.cumsum() <= input_integer].sum()
print(output_list)

Output:

[ 5  9 11 12]

What i did there:

Since total sum is to be subtracted from starting, using cumulated sum will tell you where to crop the list from.

Then set the first element = first elem - (input_integer - cropped list's sum)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very Much TJCWorld. It would be great if it could have done without modules.
2

Recursive solution:

def subfrom( lst, n ):
    if n<=0 or lst == []:
        # No change necessary
        return lst
    if lst[0] <= n:
        # First element gets removed
        # Return the list you get by removing the leftover from the rest of the list
        return subfrom( lst[1:], n-lst[0] )
    # Reducde first element
    return [lst[0]-n] + lst[1:]

1 Comment

Great Scott, Thank you very much
1

Another solution:

# First set of test variables
list1 = [3,7,9,11,12]
input1 = 2

# Second set of test variables
list2 = [3,7,9,11,12]
input2 = 5

def eval_list(input_value, iterable, output = None):
    if output is None:
        output = []

    for i, v in enumerate(iterable):
        current = iterable[i]
        if input_value > 0:
            if v <= input_value:
                input_value -= current
            else:
                current -= input_value
                input_value = 0
                output.append(current)
        else:
            output.append(current)

    return output

Run for each data set and output results:

res1 = eval_list(input1, list1)
res2 = eval_list(input2, list2)

print(f"Set 1: {res1}")
print(f"Set 2: {res2}")

Output:

Set 1: [1, 7, 9, 11, 12]
Set 2: [5, 9, 11, 12]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.