In this article, we will continue to review various functions within the Python programming language which are related to Python lists.
Additionally, we will also demonstrate functions which are native to the “Numpy” package (pronounced: “Num-Pie”).
Import a Package
To employ the functions included within the in the “Numpy” package, we must first import the “Numpy” package, which essentially invokes the library and allows the aspects therein to be utilized. If the Anaconda distribution is the Python distribution which you are operating within, the “Numpy” package is included within the distribution itself. If you are not utilizing the Anaconda distribution, the “Numpy” package must be separately downloaded.
To import, or invoke a package (library) for utilization, you must include the following line within your Python code prior to utilizing a function related to such within your code:
import <name of the package>
In the case of “Numpy”, the code would resemble:
import numpy
Numpy Arrays
Numpy arrays differ from typical Python list variables, each variable type possesses benefits dependent on the circumstances which necessitate their existence.
For a variable to be utilized in a manner which is enabled specifically through the “Numpy” package, it must first be transformed into a numpy array type variable.
# “Numpy” Example #
# Import numpy package #
import numpy
# Create list variables #
a = ["apple", "orange", "pear", "kiwi", "mango"]
b = [0, 1, 2, 3, 4]
# Create numpy array type variables #
anumpy = numpy.array(a)
bnumpy = numpy.array(b)
# What occurs when you multiply a traditional list variable by 2 #
print(b * 2)
Console Output:
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
# What occurs when you multiply a numpy array by 2 #
print(bnumpy * 2)
Console Output:
[0 2 4 6 8]
Additionally, “Numpy” provides the option to assess values through comparison operators.
# Identify all values contained within array “b” which are less than 2 #
# Re-create list variables #
b = [0, 1, 2, 3, 4]
# Create numpy array type variable #
bnumpy = numpy.array(b)
# Assess values within the array #
c = bnumpy < 2
# Print the result #
print(c)
Console Output:
[ True True False False False]
# Create an array which satisfies the above assessment #
d = bnumpy[c]
# Print the result #
print(d)
Console Output:
[0 1]
Sorting Python Lists
The following are Python functions which can be utilized to sort Python lists.
# A list can only be sorted if it contains exclusively, numeric values OR string values #
# Create list variables #
a = ["apple", "orange", "pear", "kiwi", "mango"]
b = [0, 1, 2, 3, 4]
# Reverse the list order #
a = a[::-1]
print(a)
Console Output:
['mango', 'kiwi', 'pear', 'orange', 'apple']
b.reverse
b= b[::-1]
print(b)
Console Output:
[4, 3, 2, 1, 0]
# Re-create list variables #
a = ["apple", "orange", "pear", "kiwi", "mango"]
b = [0, 1, 2, 3, 4]
# Sort list “A” in reverse alphabetical order #
c = sorted(a, reverse=True)
print(c)
Console Output:
['pear', 'orange', 'mango', 'kiwi', 'apple']
# Sort list “B” in order from greatest to least #
d = sorted(b, reverse=True)
print(d)
Console Output:
[4, 3, 2, 1, 0]
# Re-create list variables #
a = ["apple", "orange", "pear", "kiwi", "mango"]
b = [0, 1, 2, 3, 4]
# Sort list "A" in alphabetical order #
c = sorted(a, reverse=False)
print(c)
Console Output:
['apple', 'kiwi', 'mango', 'orange', 'pear']
# Sort list “B” in order from least to greatest #
d = sorted(b, reverse=False)
print(d)
Console Output:
[0, 1, 2, 3, 4]
Calling Specific List Elements
There may be instance in which you wish to call a specific element in a list without knowing what that element initially is. To identify a list element and have its value returned, you may utilize the following functions.
# Create list variables #
a = ["apple", "orange", "pear", "kiwi", "mango"]
b = [0, 1, 2, 3, 4]
# Print the largest numeric element within a set #
print(max(b))
Console Output:
4
# Print the smallest numeric element within a set #
print(min(b))
Console Output:
0
# Print the last element of a set as sorted alphabetically #
print(max(a))
Console Output:
pear
# Print the first element of a set as sorted alphabetically #
print(min(a))
Console Output:
apple
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.