python - Alternating sort column -
came around weird requirement...
suppose have model this:
class mymodel(models.model): priority = models.integerfield() time = models.datetimefield()
assume have:
by_time = mymodel.objects.order_by('time') by_prio = mymodel.objects.order_by('priority')
i need present items alternating sort order. e.g. 1 item sorted time, 1 sorted priority, next time, , on... each item needs unique in list (can't join 2 lists twice longer one).
how can implement such thing , keep relatively efficient?
edit: few notes:
- the items in ending list must unique, can't merge 2 lists.
- if merge lists , remove duplicates afterwards, sort order ruined (e.g. 2 adjacent values same order).
you can combine 2 lists way:
>>> result = [none]*(len(by_time)+len(by_prio)) >>> result[::2] = by_time >>> result[1::2] = by_prio >>> result = [result.pop(i) in range(len(result))[::-1] if result.count(result[i]) > 1 ]
Comments
Post a Comment