English 中文(简体)
按时间值分割结果列表
原标题:Slicing a result list by time value

我得到了一个结果列表,希望保留比时间线更新和比书签旧的元素。有没有比迭代整个列表并删除符合条件的元素更方便的方法?你能给我介绍一下如何具体吗?我获取数据然后进行排序的方式

results = A.all().search(self.request.get( q )).filter("published =", True)
results = sorted(results, key=lambda x: x.modified, reverse=True)

然后,我想保留比bookmark和timeline更早的元素,其中这些变量由HTTPGET定义,或者如果为空,则定义为

bookmark = datetime.now()
timeline = datetime.now () - timedelta (days = 50)

我希望你能理解我想做的事情(就像分页一样),并提前感谢你的任何想法。

最佳回答

如果您正在使用SearchableModel并进行搜索查询(从片段中可以看出您是这样的),则无法在不需要分解索引的情况下应用排序顺序或不等式过滤器,正如您在上一个主题问题中所述。因此,您不能将这些过滤器作为查询的一部分来应用,因此手动过滤结果是最好的(也是唯一的)选择。

问题回答

bookmark = datetime.now()
timeline = datetime.now () - timedelta (days = 50)


results = A.all().search(self.request.get( q )).filter("published =", True)
results = results.filter( modified__gte=bookmark ).filter( modified__lte=timeline )
results = sorted(results, key=lambda x: x.modified, reverse=True)

还是这个?

bookmark = datetime.now()
timeline = datetime.now () - timedelta (days = 50)

results = A.all().search(self.request.get( q )).filter("published =", True)
results = [ r for r in results if r.modified >= bookmark and r.modified <= timeLine ]
results = sorted(results, key=lambda x: x.modified, reverse=True)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签