python - Sort object dict by not-always-there attribute -


i want sort object dictionary in python. these objects have attribute, let's call "architecture". so, sort dict this:

data.sort(key=attrgetter('architecture')) 

so far good.

but, objects not have attribute 'architecture' (sometimes yes, no) , python console raises attributeerror exception.

so, question is, how can sort object dictionary attribute when of objects not have attribute sort?

suppose want put without attribute last.

from operator import attrgetter  data_with_attr = list(d d in data.values() if hasattr(d, 'architecture')) data_with_attr.sort(key=itemgetter('architecture')) data_with_attr += list(d d in data.values() if not hasattr(d, 'architecture')) 

i'm not sure understand object dictionary correctly.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -