Skip to content

Instantly share code, notes, and snippets.

@kmwenja
Created August 5, 2014 12:24
Show Gist options
  • Save kmwenja/d74de4a68aee4b2ae220 to your computer and use it in GitHub Desktop.
Save kmwenja/d74de4a68aee4b2ae220 to your computer and use it in GitHub Desktop.
Difference between the column value of 2 rows
def row_diff(rows, column_position):
"""
gets the difference between 2 row values
"""
# the column position parameter starts to count from 1
# so have to make it start counting from 0
column_position = column_position - 1
results = []
prev_value = None
for row in rows:
new_row = list(row)
if prev_value:
diff = new_row[column_position] - prev_value
new_row.append(diff)
else:
new_row.append(0)
prev_value = new_row[column_position]
results.append(new_row)
return results
def main():
# assume the first item in a row is an id or something
rows = (
(1, 20),
(2, 45),
(3, 78),
(4, 104),
)
results = row_diff(rows, column_position=2)
print results
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment