How to strip a list of tuple with python?
Asked Answered
B

2

1

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.

Batish answered 12/10, 2009 at 7:53 Comment(2)
Your question is rather unclear. Besides, those colspan values are freakyFaubion
This colspan value are for this simple example. The colspan value is the number of contiguous case that have the same flag.Batish
L
4
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]
Legate answered 12/10, 2009 at 8:6 Comment(3)
The colspan should restart each time. This function give me [{'colspan': 12, 'open': False}, {'colspan': 72, 'open': True}, {'colspan': 96, 'open': False}]Batish
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!Legate
Yes, I fixed it myself as well :)Batish
E
4

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}]
Ecumenical answered 12/10, 2009 at 8:18 Comment(4)
I don't know why the last colspan doesn't have the expected value.Batish
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.Batish
+1 Batteries included! Using itertools.groupby is the way to go!Legate
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}]Batish

© 2022 - 2024 — McMap. All rights reserved.