-
-
Save anonymous/ed315c65da92fd685a9668c74402c7fc to your computer and use it in GitHub Desktop.
Finding differences in values between days.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
days = [10, 11, 12, 13 , 14] | |
values = [1, 2, 3, 5, 6] | |
data = list(zip(days, values)) | |
print(data) | |
biggest_change = 0 | |
day_of_biggest_change = None | |
for index, data_point in enumerate(data[1:]): | |
print('data_point: ', data_point) | |
data_point_previous = data[index] | |
change = data_point[1] - data_point_previous[1] | |
if change > biggest_change: | |
biggest_change = change | |
day_of_biggest_change = data_point[0] | |
print(biggest_change) | |
print(day_of_biggest_change) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Using range (like a traditional for-loop in other languages) | |
""" | |
days = [10, 11, 12, 13 , 14] | |
values = [1, 2, 3, 5, 6] | |
data = list(zip(days, values)) | |
print(data) | |
biggest_change = 0 | |
day_of_biggest_change = None | |
for i in range(len(data) - 1): | |
previous = data[i][1] | |
current = data[i+1][1] | |
change = current - previous | |
if change > biggest_change: | |
biggest_change = change | |
day_of_biggest_change = data[i+1][0] | |
print(biggest_change) | |
print(day_of_biggest_change) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
using named tuples for nicer usage of data. It avoids using [0] and [1] to access values, and instead uses names. | |
""" | |
from collections import namedtuple | |
Point = namedtuple('Point', ['day', 'value']) | |
days = [10, 11, 12, 13 , 14] | |
values = [1, 2, 3, 5, 6] | |
raw_data = list(zip(days, values)) | |
data = [Point(day=day, value=value) for day, value in raw_data] | |
biggest_change = 0 | |
day_of_biggest_change = None | |
for i in range(len(data) - 1): | |
previous = data[i].value | |
current = data[i+1].value | |
change = current - previous | |
if change > biggest_change: | |
biggest_change = change | |
day_of_biggest_change = data[i+1].day | |
print(biggest_change) | |
print(day_of_biggest_change) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment