限制排序方法的输出

发布于 2024-07-29 14:55:54 字数 725 浏览 7 评论 0原文

如果我的视图代码是:

arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True)

将结果限制为 50 个标签的参数是什么?

我假设这

.... limit=50)

是不正确的。

更完整的代码如下:

videoarttags = Media.objects.order_by('date_added'),filter(topic__exact='art') 
audioarttags = Audio.objects.order_by('date_added'),filter(topic__exact='art') 
conarttags = Concert.objects.order_by('date_added'),filter(topic__exact='art') 
arttags = list(chain(videoarttags, audioarttags, conarttags)) 
arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True) 

如何合并 –

itertools.islice(sorted(...),50)

if my views code is:

arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True)

what is the argument that will limit the result to 50 tags?

I'm assuming this:

.... limit=50)

is incorrect.

more complete code follows:

videoarttags = Media.objects.order_by('date_added'),filter(topic__exact='art') 
audioarttags = Audio.objects.order_by('date_added'),filter(topic__exact='art') 
conarttags = Concert.objects.order_by('date_added'),filter(topic__exact='art') 
arttags = list(chain(videoarttags, audioarttags, conarttags)) 
arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True) 

how do incorporate –

itertools.islice(sorted(...),50)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幸福%小乖 2024-08-05 14:56:02

您可能还想将 [:50] 添加到每个 objects.order_by.filter 调用中。 这样做意味着您只需在 Python 中对内存中的 150 个项目进行排序,而不是更多。

You might also want to add [:50] to each of the objects.order_by.filter calls. Doing that will mean you only ever have to sort 150 items in-memory in Python instead of possibly many more.

草莓酥 2024-08-05 14:56:01

我想我几乎是找错了树。 我试图完成的事情实际上非常简单,使用模板过滤器(切片),但我不知道我能做到。
代码如下:

{% for arttag in arttags|slice:":50" %}

是的,我觉得很愚蠢,但我很高兴我完成了:-)

I think I was pretty much barking up the wrong tree. What I was trying to accomplish was actually very simple using a template filter (slice) which I didn't know I could do.
The code was as follows:

{% for arttag in arttags|slice:":50" %}

Yes, I feel pretty stupid, but I'm glad I got it done :-)

哭泣的笑容 2024-08-05 14:56:00

我相信,您想要的总体思路是take。 来自 itertools 文档

def take(n, iterable):
    "Return first n items of the iterable as a list"
    return list(islice(iterable, n))

The general idea of what you want is a take, I believe. From the itertools documentation:

def take(n, iterable):
    "Return first n items of the iterable as a list"
    return list(islice(iterable, n))
庆幸我还是我 2024-08-05 14:55:59

您可能会发现切片适合您:

arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True)[:50]

You'll probably find that a slice works for you:

arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True)[:50]
旧伤还要旧人安 2024-08-05 14:55:58

heapq.nlargest 怎么样:
返回由 iterable.key 定义的数据集中包含 n 个最大元素的列表,如果提供,则指定一个参数函数,用于从可迭代中的每个元素中提取比较键: key=str.lower 等价到:已排序(可迭代,key=key,reverse=True)[:n]

>>> from heapq import nlargest
>>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
>>> nlargest(3, data)
[9, 8, 7]

what about heapq.nlargest:
Return a list with the n largest elements from the dataset defined by iterable.key, if provided, specifies a function of one argument that is used to extract a comparison key from each element in the iterable: key=str.lower Equivalent to: sorted(iterable, key=key, reverse=True)[:n]

>>> from heapq import nlargest
>>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
>>> nlargest(3, data)
[9, 8, 7]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文