Skip to content

Instantly share code, notes, and snippets.

/example1.py Secret

Created December 20, 2017 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ed315c65da92fd685a9668c74402c7fc to your computer and use it in GitHub Desktop.
Save anonymous/ed315c65da92fd685a9668c74402c7fc to your computer and use it in GitHub Desktop.
Finding differences in values between days.
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)
"""
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)
"""
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