Dado este diccionario:
{
'a': {'maxVal': 2653, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
'b': {'maxVal': 2775, 'startX': 577, 'startY': 678, 'endX': 600, 'endY': 701},
'c': {'maxVal': 3431, 'startX': 610, 'startY': 733, 'endX': 632, 'endY': 755},
'd': {'maxVal': 1907, 'startX': 577, 'startY': 705, 'endX': 600, 'endY': 727},
'e': {'maxVal': 2489, 'startX': 610, 'startY': 705, 'endX': 632, 'endY': 727},
'f': {'maxVal': 1812, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
'g': {'maxVal': 3888, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783}
}
Necesito hacer un poco de "limpieza". Necesito iterar a través de él y comparar elementos donde las coordenadas son las mismas y mantener el que tenga el valor máximo más alto.
En el ejemplo dado, mantendría b, c, d,e and g
3 respuestas
Puede crear un dict con los coords como claves, y la clave y el dict con el mayor maxVal
visto para estos coords como valor. Luego, puede construir su salida a partir de él. Como no se trata de ordenar, esto funcionará en O (n).
data = {
'a': {'maxVal': 2653, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
'b': {'maxVal': 2775, 'startX': 577, 'startY': 678, 'endX': 600, 'endY': 701},
'c': {'maxVal': 3431, 'startX': 610, 'startY': 733, 'endX': 632, 'endY': 755},
'd': {'maxVal': 1907, 'startX': 577, 'startY': 705, 'endX': 600, 'endY': 727},
'e': {'maxVal': 2489, 'startX': 610, 'startY': 705, 'endX': 632, 'endY': 727},
'f': {'maxVal': 1812, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
'g': {'maxVal': 3888, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783}
}
by_coords = {}
for key, v in data.items():
coords = (v['startX'], v['startY'], v['endX'], v['endY'])
max_val = v['maxVal']
if by_coords.setdefault(coords, (key, v))[1]['maxVal'] < max_val:
by_coords[coords] = (key, v)
out = {}
for coords, (key, v) in by_coords.items():
out[key] = v
print(out.keys())
# dict_keys(['g', 'b', 'c', 'd', 'e'])
# They are the ones we expected.
print(out)
#{'g': {'maxVal': 3888, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
# 'b': {'maxVal': 2775, 'startX': 577, 'startY': 678, 'endX': 600, 'endY': 701},
# 'c': {'maxVal': 3431, 'startX': 610, 'startY': 733, 'endX': 632, 'endY': 755},
# 'd': {'maxVal': 1907, 'startX': 577, 'startY': 705, 'endX': 600, 'endY': 727},
# 'e': {'maxVal': 2489, 'startX': 610, 'startY': 705, 'endX': 632, 'endY': 727}}
# your dictionary
x = {'a': {'maxVal': 2653, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
'b': {'maxVal': 2775, 'startX': 577, 'startY': 678, 'endX': 600, 'endY': 701},
'c': {'maxVal': 3431, 'startX': 610, 'startY': 733, 'endX': 632, 'endY': 755},
'd': {'maxVal': 1907, 'startX': 577, 'startY': 705, 'endX': 600, 'endY': 727},
'e': {'maxVal': 2489, 'startX': 610, 'startY': 705, 'endX': 632, 'endY': 727},
'f': {'maxVal': 1812, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
'g': {'maxVal': 3888, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783}
}
temp = {}
for key in x.keys():
# creating cordinate map
cord = '{}-{}-{}-{}'.format(x[key]['startX'],x[key]['endX'],x[key]['startY'],x[key]['endY'])
if cord not in temp.keys():
temp[cord] = [key]
else:
key1 = temp[cord][0]
# checking the max value
if x[key1]['maxVal'] < x[key]['maxVal']:
temp[cord] = [key]
print(temp.values())
# dict_values([['g'], ['b'], ['c'], ['d'], ['e']])
Nosotras podemos hacer algo como esto:
d = {
'a': {'maxVal': 2653, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
'b': {'maxVal': 2775, 'startX': 577, 'startY': 678, 'endX': 600, 'endY': 701},
'c': {'maxVal': 3431, 'startX': 610, 'startY': 733, 'endX': 632, 'endY': 755},
'd': {'maxVal': 1907, 'startX': 577, 'startY': 705, 'endX': 600, 'endY': 727},
'e': {'maxVal': 2489, 'startX': 610, 'startY': 705, 'endX': 632, 'endY': 727},
'f': {'maxVal': 1812, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
'g': {'maxVal': 3888, 'startX': 610, 'startY': 761, 'endX': 632, 'endY': 783},
}
sorted_keys = sorted(d.keys(), key=lambda x: d[x]['maxVal'], reverse=True)
coordinates = set()
ans_keys = []
for key in sorted_keys:
my_coordinates = (
d[key]['startX'],
d[key]['startY'],
d[key]['endX'],
d[key]['endY'],
)
if my_coordinates not in coordinates:
ans_keys.append(key)
coordinates.add(my_coordinates)
print(ans_keys)
Salida:
['g', 'c', 'b', 'e', 'd']
Preguntas relacionadas
Nuevas preguntas
python
Python es un lenguaje de programación multipropósito, de tipificación dinámica y de múltiples paradigmas. Está diseñado para ser rápido de aprender, comprender y usar, y hacer cumplir una sintaxis limpia y uniforme. Tenga en cuenta que Python 2 está oficialmente fuera de soporte a partir del 01-01-2020. Aún así, para preguntas de Python específicas de la versión, agregue la etiqueta [python-2.7] o [python-3.x]. Cuando utilice una variante de Python (por ejemplo, Jython, PyPy) o una biblioteca (por ejemplo, Pandas y NumPy), inclúyala en las etiquetas.