Skip to content

Instantly share code, notes, and snippets.

@rknLA
Created October 11, 2015 20:45
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 rknLA/8b0bae3c28c49e819761 to your computer and use it in GitHub Desktop.
Save rknLA/8b0bae3c28c49e819761 to your computer and use it in GitHub Desktop.
distances = []
i = 0
while i < len(sorted_forces) — 2:
this_val = sorted_forces[i]
next_val = sorted_forces[i+1]
# ^^ this is why we use -2 instead of -1 in the while condition
distance = next_val — this_val
distances.append(distance)
i += 1
@BioGeek
Copy link

BioGeek commented Oct 12, 2015

You could write this much more succinctly using a list comprehension:

distances = [next_val - this_val for this_val, next_val in zip(sorted_forces, sorted_forces[1:])]

@rknLA
Copy link
Author

rknLA commented Oct 12, 2015

Thanks! I always forget about zip!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment