As was discussed in the prior entry, there may be instances in which you wish to call a specific list element without initially being aware of the element’s place within the list. Conversely, there may also be instances in which you wish to call an element by position without being aware of what element specifically coincides with that position.
Calling Specific List Elements (cont.)
# Index Functions #
# Create list variables #
Calling Specific List Elements (cont.)
# Index Functions #
# Create list variables #
a = ["apple", "orange", "pear", "kiwi", "mango"]
b = [0, 1, 2, 3, 4]
# Call and display variable by position #
print(b.index(3))
# Call variable and display position #
print(a.index("pear"))
Coinciding Console Output:
3
2
Calling Elements from Multi-Dimensional Lists
Though you are unlikely to encounter this data type variation within your career as a data technician, for the sake of proficiency, we will briefly discuss how to query from multi-dimensional list types.
# Create Multi-Dimensional List Variable #
fruits = [["apple", "orange"], ["kiwi", "mango"], ["cherry", "strawberry"]]
# Import numpy package #
import numpy
# Store the new list variable as numpy array type #
fruitsnumpy = numpy.array(fruits)
# Print out the third row of the numpy array type variable #
print(fruitsnumpy[2,:])
# Print out the second column of the numpy array type variable #
print(fruitsnumpy[:,1])
# Print out the second element of the first list within the numpy array type variable #
print(fruitsnumpy[:1,1])
# Print out the second element of the third list within the numpy array type variable #
print(fruitsnumpy[2,1:])
Associated Console Output:
['cherry' 'strawberry']
['orange' 'mango' 'strawberry']
['orange']
['strawberry']
Utilizing numpy.logical to logically assess Numpy Arrays
Within the numpy package, there exists two variations of a single function which allows for the logical assessment of numpy array data. Data can be assessed to either coincide with augments which satisfy multiple AND-or-OR scenarios, each exclusively. This is better demonstrated below.
# Utilizing the numpy logical function to logically assess numpy data lists #
# Import numpy package #
import numpy
# Create test set #
testset = [5,10,15,20,25,30,35,40,45,50]
# Transition test set into numpy array #
testset = numpy.array(testset)
# Return the Boolean output related to the assessment below #
# testset > 20 OR testset < 10 #
print(numpy.logical_or(testset > 20, testset < 10))
# Return the Boolean output related to the assessment below #
# testset > 20 AND testset < 45 #
print(numpy.logical_and(testset > 20, testset < 45))
Associated Console Output:
[ True False False False True True True True True True]
[False False False False True True True True False False]
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.