简体   繁体   中英

Finding Max and Min coordinates for X and Y in a dictionary

Working with a dictionary of the form {name: (x, y)}, I need to return the maximum and minimum of both the x and y coordinates, so 4 values in total.

The code I'm attempting to use is a solution to a similar question I found and tried to adapt (it seems to work for the X value, but not the Y):

maxY = particleCoords[max(particleCoords.iteritems(), key=operator.itemgetter(0))[0]][1]
minY = particleCoords[min(particleCoords.iteritems(), key=operator.itemgetter(0))[0]][1]

(the name of the dictionary is particleCoords)

The numbers are floats, and can be negative.

I know there are a lot of similar questions to this, but the one I did find that seemed to be the same thing I couldn't adapt.

Thanks for any help.

seems to me like:

min_x = min(particleCoords.iteritems(),key = lambda item: item[1][0])
max_x = max(particleCoords.iteritems(),key = lambda item: item[1][0])

min_y = min(particleCoords.iteritems(),key = lambda item: item[1][1])
max_y = max(particleCoords.iteritems(),key = lambda item: item[1][1])

should work...

The comparison of each item is based on the value ( item[1] ) portion of each item. Then depending on whether we want x or y we either compare the first or second element in the value .

This will give you the key-value pair which is minimal/maximal depending on the particular key function. If you don't care about getting the names, you can do this a little more cleanly:

min_x = min(particleCoords.itervalues(),key = itemgetter(0))
max_x = max(particleCoords.itervalues(),key = itemgetter(0))

min_y = min(particleCoords.itervalues(),key = itemgetter(1))
max_y = max(particleCoords.itervalues(),key = itemgetter(1))

Again the min_x and max_x are actually the (x,y) pair which hold the minimal and maximal x respectively.

This would also work:

>>> d = {'name1': (1, 6), 'name2': (7, 4), 'name3': (10, 1)}
>>> v = d.values()
>>> zipped = zip(*v)
>>> print min(zipped[0]), max(zipped[0]), min(zipped[1]), max(zipped[1])
1 10 1 6

Here is another solution using zip and list comprehension as long as you don't need to map the value to the keys. However, I'm not sure whether it would be faster or slower than the previously mentioned methods.

(x_max, x_min),  (y_max, y_min) = [(max(x), min(x)) for x in zip(*my_dict.values())]

Not sure if I interpret your question correctly but if you want to get the extreme values from all tuples in a dictionary/associative array of tuples of the form {<key>: (x, y)} , then this might give you what you need:

my_dict = {
           "bananas": (3, 5),
           "apples": (2, 5),
           "oranges": (4, 7)
           }

x_min = None
x_max = None
y_min = None
y_max = None

for x in my_dict:
    try:
        if my_dict[x][0] < x_min: x_min = my_dict[x][0]
        elif my_dict[x][0] > x_min: x_max = my_dict[x][0]
        if my_dict[x][1] < y_min: y_min = my_dict[x][1]
        elif my_dict[x][1] > y_max: y_max = my_dict[x][1]
    except:
        x_min = my_dict[x][0]
        x_max = my_dict[x][0]
        y_min = my_dict[x][1]
        y_max = my_dict[x][1]

print("Extremes (min, max) for position (x, y) are: ((%s, %s), (%s, %s))" \
      % (x_min, x_max, y_min, y_max))

Is that what you were after?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM