The the place() technique in Python is used to examine for a selected situation on each ingredient of an array after which select a respective worth to be positioned at that ingredient’s index relying upon the results of the situation. Nevertheless, to use the “the place()” technique on an inventory, the listing needs to be transformed into an array first
The “the place()” technique just isn’t a built-in technique of the Python language, it really belongs to the “Numpy” bundle. To grasp the working of the “the place()” technique, check out its syntax:
Syntax of the “the place()” Methodology
The syntax of the “the place()” technique may be demonstrated as:
np.the place(situation, value1 , value2)
Within the above syntax:
- situation to be checked on each merchandise of the listing contained in the the place() technique
- value1: to be positioned if the situation outlined within the the place() technique yield true
- value2: for use if the situation in the place() technique yields false
Further Info
For the “the place()” technique, the next facets are vital and have to be adopted
- The value1 and value2 are optionally available parameters. Nevertheless, both each of them are offered or none.
- The value1 and the value2 parameters generally is a static worth, static string, or array. Nevertheless, if these values are array then the form and the dimensions of the array ought to match the array within the situation
Instance 1: Fetch Parts That Match the Situation
On this first instance, you will take an inventory and fetch solely these parts that match a sure situation. To do that, first, create an inventory and use the next line:
numbers = (1,66,2,23,91,14,51,47)
The duty is to solely select the weather which have a price increased than “50”, for that, convert the listing into an array after which use the next situation within the “the place()” technique:
outcome= (np.the place(array > 50))
Lastly, print out the outcome on the terminal:
The entire code snippet for this instance is as:
numbers = (1,66,2,23,91,14,51,47)
array = np.array(numbers)
outcome = (np.the place(array > 50))
print(outcome)
Upon working this code, you’ll be able to see that the output is:
The output shows the “index” values of the ingredient that meet the situation within the “the place()” technique.
- Index 1 = 66
- Index 4 = 91
- Index 6 = 51
Instance 2: Offering Each Circumstances for Situation
On this instance, you will present each of the actions which are to be carried out relying upon the outcome yielded by the situation. To exhibit this, take the next marks of scholars in a sure topic:
scores= (52,46,91,33,52,67,51,47)
After that, apply the circumstances that if the rating of the scholar is above 50, grant the scholar the standing of “Go”, in any other case “Fail”
outcome =(np.the place(array > 50 , “Go”,“Fail”))
After that, show each of the listing, authentic and outcome, on the terminal utilizing the next traces of code:
print(“Verdict: “, outcome)
The entire code snippet for this instance could be:
scores = (52,46,91,33,52,67,51,47)
array = np.array(scores)
outcome =(np.the place(array > 50 , “Go”,“Fail”))
print(“Authentic Listing: “, scores)
print(“Verdict: “, outcome)
Working this code goes to provide the next outcome on the terminal:
Instance 3: Utilizing Arrays as Situation
On this instance, carry out the situation in your listing, and primarily based upon that situation, decide the worth from both the array within the second parameter of the the place() technique or the array within the third parameter.
Begin by importing the numpy bundle after which create an inventory utilizing the next traces:
scores = (52,46,91,33,52,67,51,47)
If the worth is bigger than 50, then take the worth from the second parameter’s array, and if its decrease then use the array within the third parameter:
outcome =(np.the place(array > 50 ,
[1,2,3,4,5,6,7,8],
[11,12,13,14,15,16,17,18]))
And at last, print the arrays on the terminal:
print(“New Listing: “, outcome)
The entire code snippet for this instance is as:
scores = (52,46,91,33,52,67,51,47)
array = np.array(scores)
outcome =(np.the place(array > 50 ,
[1,2,3,4,5,6,7,8],
[11,12,13,14,15,16,17,18]))
print(“Authentic Listing: “, scores)
print(“New Listing: “, outcome)
This code yields the next outcome on the terminal:
From the output, it may be seen that for each ingredient larger than 50, the brand new worth is lower than 10 (second parameter), and for each ingredient decrease than 50, the brand new worth is now larger than 10 (third parameter)
Instance 4: Performing Operations Based mostly on Situation
On this instance, take the next listing:
listing= (1,2,3,4,5,6,7,8,9)
If the merchandise is even, take it as it’s, whether it is odd, multiply it with 10 utilizing:
outcome =(np.the place(array %2 ==0 ,array,array*10)
After which print the outcome on the terminal:
print(“New Listing: “, outcome)
The entire code snippet for this instance is that this:
listing = (1,2,3,4,5,6,7,8,9)
array = np.array(listing)
outcome =(np.the place(array %2 ==0 ,array,array*10)
print(“Authentic Listing: “, listing)
print(“New Listing: “, outcome)
Working this above-mentioned code snippet will produce the next outcome:
The output confirms that each odd ingredient was multiplied by 10.
Conclusion
The “the place()” technique acts as a type of a filter that filters the ingredient relying upon the situation, and primarily based on this situation it might probably carry out actions on the ingredient. To make use of the “the place()” technique, the person must import the Numpy library because the “the place()” technique just isn’t a built-in technique of Python. Additionally, the listing must be transformed to a Numpy array earlier than making use of the the place() technique on it.