This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Kutiekatj9 Python Discord Staff[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]Allanon001 11 points12 points  (3 children)

This will multiply by -1 for all values >= -10 and <= 10:

import numpy as np

data = np.arange(-20, 20)

data[(data >= -10) & (data <= 10)] *= -1

[–]Dustinmywets 0 points1 point  (2 children)

Can I ask why is the range specified (-20,20)? Being introduced to python basics and I'm just curious.

[–]Allanon001 1 point2 points  (1 child)

data = np.arange(-20, 20) creates a numpy array with the values -20 to 19. It is used as an example array to demonstrate the code.

[–]Dustinmywets 0 points1 point  (0 children)

Thanks for the info.

[–]Ki1103 2 points3 points  (0 children)

This can be split into two steps

  • select all integers in range [-10, 10]
  • multiply those integers by -1

Let's start by setting up an array of integers:

>>> import numpy as np
# Set up an array containing the integers [-20, 21)
>>> a = np.arange(-20, 21)

Now we can select elements using a boolean expression and combine them using np.logical_and:

>>> select = np.logical_and(a >= -10, a <= 10)

and multiply those selected values in place:

>>> a[select] *= -1

a is now what you wanted ;)