开发者

How to strip a list of tuple with python?

开发者 https://www.devze.com 2022-12-08 20:25 出处:网络
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 :

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, 'o开发者_运维知识库pen': 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.


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]


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}]
0

精彩评论

暂无评论...
验证码 换一张
取 消