[pyar] como ordenar y "limpiar" una lista de diccionarios

Facundo Batista facundobatista en gmail.com
Mie Oct 5 17:09:19 ART 2011


2011/10/5 Otto Federico Wald <ofwald en gmail.com>:

> valores_inicio=[{a:1, b:2}, {a:2, b:3}, {a:3, b:3}, {a:4, b:3}, {a:5 , b:4}]
> a esto:
> valores_final=[{a:1, b:2}, {a:4, b:3}, {a:5 , b:4}]

>>> ini = [{'a':1, 'b':2}, {'a':2, 'b':3}, {'a':3, 'b':3}, {'a':4, 'b':3}, {'a':5 , 'b':4}]
>>> d = {}
>>> for it in ini:
...   d.setdefault(it['b'], []).append(it['a'])
...
>>> [{'b':k, 'a':max(v)} for k, v in d.items()]
[{'a': 1, 'b': 2}, {'a': 4, 'b': 3}, {'a': 5, 'b': 4}]

-- 
.    Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/



More information about the pyar mailing list