Group query elements by same name in django -


i stuck on task. query output follows (when list() used):

days=[{'day':1,'do':'paint'},{'day':2,'do':'paint'},{'day':3,'do':'dry'},{'day':4,'do':'fubrish'},{'day':5,'do':'fubrish'},{'day':6,'do':'paint'}] 

it comes model:

class tasks(models.model):     day_no = models.integerfield(verbose_name='number of day')  # 1, 2, 3, 4, 5, 6    task = models.charfield(max_length=25) 

i want loop thru each , see if there same 'do' consecutively , store total number of found occurance. e.g. day 1 , 2, user advised painting. day 3 drying , next 2 days fubrish again paint.

i want output say:

paint: 2 dry: 1 fubrish: 2 paint: 1 

i did play on jupyter errorful:

tasks={}  total=len(days)  in range(0,total):     day = days[i]['day']     = days[i]['do']  if == 0:     tasks[do]=1 else:     if i+1 < total:         if days[i+1]['do'] == do:  # next 1 same current 1             tasks[do] = len(tasks) + 1         else:             if tasks.get(do, none) != none:                 tasks[do] = len(tasks) + 1             else:                 tasks = {}                 tasks[do] = 1 

you use function:

def func(task_list):     if len(task_list) == 0:         return     current = task_list[0]['do']     count = 1     in range(1, len(task_list)):         new_task = task_list[i]['do']         if new_task == current:              count+=1         else:              print '%s: %d' %(current, count)              count = 1         current = new_task     print '%s: %d' %(current, count) 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -