views:

305

answers:

2

I have an array with some flag for each case. In order to use print the array in HTML and use colspan, I need to convert this :

[{'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': True, 'open': True}, {'serve': False, 'open': True}, {'serve': False, 'open': True}, {'serve': False, 'open': True}, {'serve': False, 'open': True}, {'serve': False, 'open': True}, {'serve': False, 'open': True}, {'serve': False, 'open': True}, {'serve': False, 'open': True}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}]

In this for the open flag:

[{'colspan': 12, 'open': False}, {'colspan': 60, 'open': True}, {'colspan': 24, 'open': False}]

And another to generate the serve one.

How can I do this the smartest way using Python ?

I could count the case one by one, but it doesn't seams to be a good idea.

+3  A: 
def cluster(dicts, key):
    current_value = None
    current_span = 0
    result = []

    for d in dicts:
        value = d[key]
        if current_value is None:
            current_value = value
        elif current_value != value:
            result.append({'colspan': current_span, key: current_value})
            current_value = value
            current_span = 0
        current_span += 1

    result.append({'colspan': current_span, key: current_value})
    return result

by_open = cluster(data, 'open')
by_serve = cluster(data, 'serve')

Second version, inspired by Denis' answer and his use of itertools.groupby:

import itertools
import operator

def make_spans(data, key):
    groups = itertools.groupby(data, operator.itemgetter(key))
    return [{'colspan': len(list(items)), key: value} for value, items in groups]
Ferdinand Beyer
The colspan should restart each time. This function give me [{'colspan': 12, 'open': False}, {'colspan': 72, 'open': True}, {'colspan': 96, 'open': False}]
Natim
There was a missing line in my code that I corrected some minutes ago (`current_span = 0`). It should work as expected now. Excuse my question, but you know how to program, right? I mean, the code is just an example to get you started, not production-ready code. If you understand what the code is doing, spotting the error you described should have been pretty easy!
Ferdinand Beyer
Yes, I fixed it myself as well :)
Natim
+4  A: 

This is not clear what you need, but I hope the following examples will help you:

>>> groupped = itertools.groupby(your_list, operator.itemgetter('open'))
>>> [{'colspan': len(list(group)), 'open': open} for open, group in groupped]
[{'colspan': 12, 'open': False}, {'colspan': 60, 'open': True}, {'colspan': 78, 'open': False}]
>>> groupped = itertools.groupby(your_list)
>>> [dict(d, colspan=len(list(group))) for d, group in groupped]
[{'serve': False, 'open': False, 'colspan': 12}, {'serve': True, 'open': True, 'colspan': 52}, {'serve': False, 'open': True, 'colspan': 8}, {'serve': False, 'open': False, 'colspan': 78}]
Denis Otkidach
I don't know why the last colspan doesn't have the expected value.
Natim
Ok, know I know ... My sample get 150 cases instead of 96 ... Python is so powerful when you know how to use it. Thank you.
Natim
+1 Batteries included! Using `itertools.groupby` is the way to go!
Ferdinand Beyer
How can I do if I want to keep one information that I know to be the same for the all group ?For example : [{'colspan': 12, 'open': False}, {'colspan': 60, 'open': True, 'schedule_id':1}, {'colspan': 78, 'open': False}]
Natim