There is not a lot of discussion on some of the tasks asked to be performed in here, specifically 6 and 7 which could be considered difficult to a newer python learner.
6. “Hmmm, interesting” the researcher mumbles, “can you also give me the 6:00 AM data for Thursday and Friday?” Save this data to a new array thursday_friday_morning
.
If you look at the two-dimensional array of:
[[ 46.6 48.1 61.8 56. ]
[ 50. 47.5 61.3 55.6]
[ 49.7 47.2 60.9 55.2]
[ 49.5 47.1 60.6 54.9]
[ 49.2 46.9 60.2 54.5]]
Then start the process by printing out something like this where we slice a start of 3, with a not inclusive stop of 5
print(temperatures_fixed[3:5])
You will get, something like below, which
[[ 49.5 47.1 60.6 54.9]
[ 49.2 46.9 60.2 54.5]]
Good start, but not what the lesson wants, it only wants the 6am temperature for Thursday/Friday, not all the temperatures. You will use ‘,’ in the slice to select the element of the second dimension, 6am is element 1.
thursday_friday_morning = temperatures_fixed[3:5,1]
or
thursday_friday_morning = np.array(temperatures_fixed[3:5,1])
both will work
7. Select all temperatures that are under 50 degrees or over 60 degrees and save them to a new array temperature_extremes
.
Past in some looping related tutorials they have shown that you can create a list from another list with some conditions using:
var = [l for l in list if <some condition with l>]
You can also perform a nested loop by doing:
var = [l for l in list for ll in l if <condition>]
So with that understanding you can do the following:
temperature_extremes = [float(t) for tf in temperatures_fixed for t in tf if t < 50 or t > 60]
Which will give you the output and the correct answer:
[46.6, 48.1, 61.8, 47.5, 61.3, 49.7, 47.2, 60.9, 49.5, 47.1, 60.6, 49.2, 46.9, 60.2]