2
counts = misc. array

    for i in counts:
        if i < "(sum of the two integers next to i)/4"
            return -1

What I am trying to do is set all values in the list which are less than half the average of their two neighbours to -1. i.e. for the list [10., 20., 10., 25., 30.] if i=3 then I want to see if 10 is less than (20+25)/4 and since that is 45/4 > 10 I want to then set 10 equal to -1. I will then filter for all values in the list >= 0.

This is further complicated by the fact that you should start with i+1 because this process doesn't work with the first value and then you should end with -1 because this process doesn't work with the last value. Any help would be greatly appreciated.

4 Answers 4

1

You could borrow the sliding window generator from this answer, and put it to use like so:

out = [y if y >= (x+z)/4. else -1 for x,y,z in window(l, 3)]

where l is your list.

This chops off the first and the last element. It would be easy to do something different to them (e.g. set to -1) if you so wish.

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

1 Comment

how does that help user(new_user_number) who just about asked his first question? Not that my answer is the greatest...
0

Does this do what you want (assuming values are in list data)?

filtered = [data[i] for i in range(1,len(data)-1) if data[i] >= (data[i+1]+data[i-1])/4]

Comments

0

I have a solution that doesn't look too elegant, but I believe is correct and understandable. The filtering is included, so it will also work if -1 is an allowed value.

I use 3 variables (a,b and i), and 3 flag variables (fa, fb and fi) which show if the value needs to be included in the result set or not.

def test(lst):
    a, fa, b, fb, res  = None, False, None, False, []
    for i in lst:
        if a is not None:
            fi = b < (a + i)/4
        else:
            fi = True
        if fa:
            res.append(a)
        a, fa = b, fb
        b, fb = i, fi
    if fa:
        res.append(a)
    res.append(b)
    return res

print(test([10., 20., 10., 25., 30.]))
=> [10.0, 20.0, 25.0, 30.0]

Comments

0

Like that:

result = [(x+1 if counts[x] > counts[x-1]/counts[x+1]/4 else -1) for x in range(len(counts[1:-1]))]

- x is a idex in counts --> for x in range(len(counts[1:-1])
- x+1 because we know counts[0] and count[-1] are out. Hence to achieve result one must adjust it.
- if count[x] > counts[x-1]/counts[x+1]/4 else -1 test, returns the result if result is True, otherwise -1
note the > 0, it's opposite to what you want
- for x in range(len(counts[1:-1]))
because we know first and last value is of no matter
- counts[1:-1] as above, always better to use a copy of a list, thread-safe

This should work, will give you a list of valid (!=-1) and invalid (== -1) results per index in count,

question is, what can you do further with the result?

you can test, e.g.: (mind the adjusted x in result)

 ['N/A'] + [(x if x!= -1 else False) for x in result] + ['N/A']

which gives you a mirror image of counts with either False or True result

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.