2. Mając dane w takiej postaci, gdzie pierwszy index to numer strony, a drugi to numer zawartości.

</h3>


In [ ]:
POST = {
    u"page[1][1]['id']": u'baloes_bd_8_1',
    u"page[0][1]['text']": u'Mum, dad! Look, the school email.',
    u"page[1][0]['id']": u'baloes_bd_9_1',
    u"page[0][1]['id']": u'baloes_bd_6_1',
    u"page[0][0]['id']": u'baloes_bd_5_1',
    u'next': u'/mycontent/5910974510923776',
    u"page[0][0]['text']": u'Some time later\u2026',
    u'space_id': u'5910974510923776',
    u"page[1][0]['text']": u'You open the email, Luana. \u2028It\u2019s for you!',
    u"page[1][1]['text']": u'Me too!',
    u'skip_editor': u'1',
}

In [ ]:
#Stwórz listę stron, która będzie zawierała kolejne zawartości 'text' w postaci krotek (id, text).

expected = [
    [(u'baloes_bd_5_1', u'Some time later\u2026'), (u'baloes_bd_6_1', u'Mum, dad! Look, the school email.')],
    [(u'baloes_bd_9_1', u'You open the email, Luana. \u2028It\u2019s for you!'), (u'baloes_bd_8_1', u'Me too!')]
]

In [ ]:
from itertools import groupby, ifilter
from collections import namedtuple


def _page_text_filter(value):
    return value.startswith("page") and value.endswith("['text']")


def _page_index_key(value):
    index = value.find("]")
    lindex = value.find("[") + 1
    return int(value[lindex:index])


def convert_post_data(post_data):
    text_only = ifilter(_page_text_filter, post_data)
    sorted_only = sorted(text_only, key=_page_index_key)
    grouped_by_page = groupby(sorted_only, _page_index_key)

    pages = []
    for page_index, group in grouped_by_page:
        texts = []
        for text_index in group:
            text_data = post_data[text_index]
            text_id = post_data[text_index.replace("text", "id")]
            texts.append(TextDataTuple(text_id, text_data))
        pages.append(list(reversed(texts)))

    return pages

TextDataTuple = namedtuple("TextDataTuple", "id text")

In [2]:
print("12" > "2")


False