Home > Software engineering >  How can i remove the negatives in an indexed bracket, when there is at least one negative and at lea
How can i remove the negatives in an indexed bracket, when there is at least one negative and at lea

Time:10-21

sumranges= [4, 4, 4, 4, 2, 2, 1, 1, 1]
amounts=[' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', '-2.5', ' 5.0', '-5.0', ' 5.0', ' 5.0', ' 25.0']

for i in range(0, len(amounts)):
    amounts[i] = float(amounts[i])
    
newarray=[]
count=0

if sum(sumranges) == len(amounts):    
    for sumrange in sumranges:
        start=int(sum(sumranges[0:count]))
        end=int(start sumrange)
        newarray.append(sum( amounts[start:end] ) )
        count =1
        
    print(newarray)
else:
    print("Sum of sumranges isn't equal to length of amounts array")

result is

[10.0, 10.0, 10.0, 10.0, 0.0, 0.0, 5.0, 5.0, 25.0]

However, result should be:

[" 10.0", " 10.0", " 10.0", " 10.0", " 2.5", " 5.0", " 5.0", " 5.0", " 25.0"]

The first 0.0 happened because sum of 2.5 -2.5 is 0. Similar thing to the second 0.0 but this time with 5 -5. How can i remove the -2.5 so it's 2.5, and for the second one result should be 5. I can't just remove all the negatives because the code should also work for this second data:

sumranges=[4,2,2,1,1,1,1]
amounts=['-2.5', '-2.5', '-2.5', '-2.5', '-2.5', '-2.5', '-5.0', '-5.0', '-5.0', '-5.0', '-25.0', ' 99.999999999']

result should be:

["-10.0", "-5.0", "-10.0", "-5.0", "-5.0", "-25.0", " 99.999999999"]

CodePudding user response:

IIUC, you could filter out your negative numbers:

sumranges= [4, 4, 4, 4, 2, 2, 1, 1, 1]
amounts=[' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', '-2.5', ' 5.0', '-5.0', ' 5.0', ' 5.0', ' 25.0']

out = []
start=0
for step in sumranges:
    end = start step
    l = list(map(float, amounts[start:end]))
    if len(set(i < 0 for i in l)) > 1:
        l = [i for i in l if i>0]
    s = sum(l)
    out.append('%s%.1f' % (' ' if s >=0 else '' , s))
    start = end
    
out

output:

[' 10.0', ' 10.0', ' 10.0', ' 10.0', ' 2.5', ' 5.0', ' 5.0', ' 5.0', ' 25.0']

output with second input:

['-10.0', '-5.0', '-10.0', '-5.0', '-5.0', '-25.0', ' 100.0']

CodePudding user response:

There's a question with this answer. This code will give the desired result for your second example (except for the formatting of the 99.99999) value.

If I apply the same data as per your first example, it gives the output that you say is wrong.

Your explanations about why and when you want to "remove" a negative value is unclear.

So, by showing you this code it might help you to clarify what you need:-

def process(s, a):
    assert sum(s) == len(a)
    n = []
    o = 0
    for _s in s:
        t = sum(list(map(float, a[o:o _s])))
        n.append(f'{t: .2f}')
        o  = _s
    return n

sumranges = [4, 4, 4, 4, 2, 2, 1, 1, 1]
amounts = [' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5',
           ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', '-2.5', ' 5.0', '-5.0', ' 5.0', ' 5.0', ' 25.0']

print(process(sumranges, amounts))

sumranges = [4, 2, 2, 1, 1, 1, 1]
amounts = ['-2.5', '-2.5', '-2.5', '-2.5', '-2.5', '-2.5',
           '-5.0', '-5.0', '-5.0', '-5.0', '-25.0', ' 99.999999999']

print(process(sumranges, amounts))

CodePudding user response:

You could use an iterator to form the range groups inside a list comprehension, then subtract the sum of negatives from the group's sum if the largest element is not a negative.

Here is a function that will do it (on a list of proper numeric values):

def sumPosRanges(amounts,sumranges):
    return [ sum(r)-sum(n for n in r if n<0)*(max(r)>0)
             for iAmounts in [iter(amounts)] for sr in sumranges
             for r in [[next(iAmounts) for _ in range(sr)]] ]

First input:

sumranges= [4, 4, 4, 4, 2, 2, 1, 1, 1]
amounts=[' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5',
         ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5', ' 2.5',
         ' 2.5', '-2.5', ' 5.0', '-5.0', ' 5.0', ' 5.0', ' 25.0']

sums = sumPosRanges(map(float,amounts),sumranges)
           
print([f"{s: 3.1f}" for s in sums])        
[' 10.0', ' 10.0', ' 10.0', ' 10.0', ' 2.5', ' 5.0', ' 5.0', ' 5.0', ' 25.0']

Second input:

sumranges=[4,2,2,1,1,1,1]
amounts=['-2.5', '-2.5', '-2.5', '-2.5', '-2.5', '-2.5', 
         '-5.0', '-5.0', '-5.0', '-5.0', '-25.0', ' 99.999999999']

sums = sumPosRanges(map(float,amounts),sumranges)
           
print([f"{s: 3.1f}" for s in sums])        
['-10.0', '-5.0', '-10.0', '-5.0', '-5.0', '-25.0', ' 100.0']
  • Related