I am very new to coding, but I have a real-life problem that I want to have some code for. I have a list of items, which each have locations - I put it into Python as something like objects = {"a": 0, "b": 7, "c": 24, "d": 2}
How do I loop through this dictionary and find the midpoints of each pair of items? I abbreviate midpoint as "x/y". So ultimately I want something that looks like, midpoints = {"a/b": 3.5, "a/c": 12, "a/d": 1, "b/c": 15.5, "b/d": 4.5, "c/d": 13}.
I know you'd start with a function and a for loop, maybe something like
def mp(x,y):
return (x+y)/2
midpoints = {}
for key1 in objects:
for key2 in objects:
midpoints[key1+"/"+key2] = mp(objects[key1],objects[key2])
but this is giving useless midpoints ("a/a") as well as repeated midpoints (e.g. both "a/b" and "b/a").
I can't figure out how to clean this up...I guess you'd have to iterate over a subset of the dictionary? Any help would be greatly appreciated! :)