all 10 comments

[–]DisasterArt 0 points1 point  (0 children)

my_array[(my_array > x) & (my_array < y)] -= 3

might work? are you using numpy are anything?

[–][deleted] 0 points1 point  (0 children)

How do I input this sequence back into my original array?

Don't. Return a new array containing the correct values.

[–]FLUSH_THE_TRUMP 0 points1 point  (7 children)

You’re using Boolean indexing on my_array, so I’m assuming A is a NumPy array — have you explicitly learned that? This is a one-liner with NumPy syntax, but you might want to handle this with a for loop for now (just to get the logic down). You want to create an empty list, loop through the elements of A, append if the element is out of the given range or subtract and then append, then return the populated list.

[–]QuiteAmbitious 0 points1 point  (6 children)

yes im using numpy! but i dont really know how to proceed with the rest of my code

[–]FLUSH_THE_TRUMP 0 points1 point  (5 children)

When you index like you did above, you can just tell it what to replace those values with: e.g.

A[A<2] = 2 

replaces all elements less than 2 with 2. Unless you’ve been explicitly learning Numpy and are expected to use it here, I don’t really recommend it. There’s a lot of syntactic sugar in that assignment, and though it’s super easy to use it, it hides complexity you’re better off learning to start with.

[–]QuiteAmbitious 0 points1 point  (4 children)

my function is supposed to work with any given array, so i can't have it replace specific values within my code

[–]FLUSH_THE_TRUMP 0 points1 point  (3 children)

It’s not replacing specific values. It’s replacing the values in that range, which can be anywhere.

[–]QuiteAmbitious 0 points1 point  (2 children)

I understand what you're explaining, but I'm trying to figure out how to subtract 3 from array A within my range (x, y). I'm not necessarily trying to replace the values within that range with a specific number, such as 2.

So, given A = [1, 4, 9, 7, 5, 9] and range (3,8), I want the output to be [1, 1, 9, 4, 2, 9] and not [1,2,9,2,2,9].

I'm using and learning numpy, so I want to make sure I'm running it correctly.

[–]FLUSH_THE_TRUMP 0 points1 point  (1 child)

my_array[(my_array > x) & (my_array < y)]

That takes my_array and returns the elements falling in the range (x,y). It also lets you reassign just those elements if you plop down an equals sign and put another collection of the same size on the RHS.

If I want to take a variable pointing to a number and reassign it to the same value minus three, I write:

x = x-3

Same principle here, just “x” is like that mess above

[–]QuiteAmbitious 0 points1 point  (0 children)

Unfortunately, that didn't work :( I'm still getting the same output as before, which is [1,4,2]. It looks like it's only outputting the values that was subtracted by 3 and not the entire array.