Is this how you paginate, or is there a better algorithm?

丶灬走出姿态 提交于 2019-11-29 12:32:28

问题


I want to be able to take a sequence like:

my_sequence = ['foo', 'bar', 'baz', 'spam', 'eggs', 'cheese', 'yogurt']

Use a function like:

my_paginated_sequence = get_rows(my_sequence, 3)

To get:

[['foo', 'bar', 'baz'], ['spam', 'eggs', 'cheese'], ['yogurt']]

This is what I came up with by just thinking through it:

def get_rows(sequence, num):
    count = 1
    rows = list()
    cols = list()
    for item in sequence:
        if count == num:
            cols.append(item)
            rows.append(cols)
            cols = list()
            count = 1
        else:
            cols.append(item)
            count += 1
    if count > 0:
        rows.append(cols)
    return rows

回答1:


If you know you have a sliceable sequence (list or tuple),

def getrows_byslice(seq, rowlen):
    for start in xrange(0, len(seq), rowlen):
        yield seq[start:start+rowlen]

This of course is a generator, so if you absolutely need a list as the result, you'll use list(getrows_byslice(seq, 3)) or the like, of course.

If what you start with is a generic iterable, the itertools recipes offer help with the grouper recipe...:

import itertools

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.izip_longest(fillvalue=fillvalue, *args)

(again, you'll need to call list on this if a list is what you want, of course).

Since you actually want the last tuple to be truncated rather than filled up, you'll need to "trim" the trailing fill-values from the very last tuple.




回答2:


This version works with any (possibly lazy and non-sliceable) iterable and produces a lazy iterable (in other words, it's a generator and works with all types of sequences, including other generators):

import itertools

def paginate(iterable, page_size):
    while True:
        i1, i2 = itertools.tee(iterable)
        iterable, page = (itertools.islice(i1, page_size, None),
                list(itertools.islice(i2, page_size)))
        if len(page) == 0:
            break
        yield page

Some examples:

In [61]: list(paginate(my_sequence, 3))
Out[61]: [['foo', 'bar', 'baz'], ['spam', 'eggs', 'cheese'], ['yogurt']]

In [62]: list(paginate(xrange(10), 3))
Out[62]: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]



回答3:


The grouper function in the itertools docs is clever and concise; the only problem is you might need to trim the results, as Alex Martelli pointed out. I would be inclined towards a solution along the lines of Michał Marczyk's answer, though I don't see why that can't be made much simpler. This works for all the cases I can conceive of:

import itertools

def paginate(seq, page_size):
    i = iter(seq)
    while True:
        page = tuple(itertools.islice(i, 0, page_size))
        if len(page):
            yield page
        else:
            return



回答4:


If you are looking for straight up list comprehension, this will do the job:

L = ['foo', 'bar', 'baz', 'spam', 'eggs', 'cheese', 'yogurt']
[L[i*3 : (i*3)+3] for i in range((len(L)/3)+1) if L[i*3 : (i*3)+3]]
# [['foo', 'bar', 'baz'], ['spam', 'eggs', 'cheese'], ['yogurt']]
L = ['foo', 'bar', 'baz', 'spam', 'eggs', 'cheese']
# [['foo', 'bar', 'baz'], ['spam', 'eggs', 'cheese']]


来源:https://stackoverflow.com/questions/3744451/is-this-how-you-paginate-or-is-there-a-better-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!