简体   繁体   English

从列表中获取两个日期之间的元素

[英]Get Elements between two dates from a list

I have a list of list which goes as follows: 我有一个列表列表,如下所示:

A = [['05-22-2013', '0.5553', '0.887', '0.14'], 
     ['05-22-2013', '0.3442', '0.345', '0.0'], 
     ['05-22-2013', '0.3', '0.7', '0.4'], 
     ['05-23-2013', '0.53', '0.87', '0.4'], 
     ['05-23-2013', '0.9', '0.8', '0.1'], 
     ['05-23-2013', '0.0', '0.799', '0.214'],
     ['05-24-2013', '0.053', '0.7', '0.1422'], 
     ['05-25-2013', '0.5', '0.110', '0.200'], 
     ['05-25-2013', '0.311', '0.799', '0.426'], 
     ['05-25-2013', '0.311', '0.091', '0.41']]

I want to select all the elements between dates of '05-22-2013' to '05-24-2013'. 我想选择“ 2013年5月22日”到“ 2013年5月24日”之间的所有元素。 What I am trying is manually getting all the elements corresponding to each date in an array and appending them all into a single list of list. 我正在尝试手动获取与数组中每个日期对应的所有元素,并将它们全部添加到单个列表中。 Like for one date: 喜欢一个日期:

date_1 = []

for u in A:
    if '05-22-2013' in u:
        date_1.append(u)

So, specifically stating, what is the best possible way to get all the items within a given date range (in this sort of list of lists)? 因此,特别说明一下,在给定日期范围内(在这种列表列表中)获取所有项目的最佳方法是什么?

I don't know if this is the "best" way, because let's be honest, "best" is a very subjective term. 我不知道这是否是“最佳”方式,因为老实说,“最佳”是一个非常主观的术语。

from datetime import date

start_date = date(2013, 5, 22)
end_date = date(2013, 5, 24)

data = []

for i in A:
    month, day, year = i[0].split('-')
    record_date = date(year, month, day)
    if record_date >= start_date and record_date <= end_date:
        data.append(i)

To simply get all list entries that match a given date, you can build a list comprehension: 要简单地获取与给定日期匹配的所有列表条目,您可以构建列表理解:

print [entry for entry in A if entry[0] == '05-25-2013']

RETURNS: 返回值:

[['05-25-2013', '0.5', '0.110', '0.200'], ['05-25-2013', '0.311', '0.799', '0.426'], ['05-25-2013', '0.311', '0.091', '0.41']]

This approach only addresses one date, of course, but could be modified in the if to handle additional dates. 当然,此方法仅处理一个日期,但是可以修改if以处理其他日期。 The downside is you'd have to explicitly list off each date in the range, as they are all interpreted as mere strings. 缺点是您必须明确列出范围内的每个日期,因为它们都被解释为纯字符串。

If you really need to be able to enter a range, though, you'll have to get more indepth and probably use the datetime module, which would convert it into a datatype capable of using > and < -like operators. 但是,如果确实需要输入范围,则必须更深入,并可能使用datetime模块,该模块将其转换为能够使用>< -运算符的数据类型。

If linear-time preprocessing is an option, then first get the keys (dates) out into a separate list. 如果可以选择线性时间预处理,则首先将键(日期)取出到单独的列表中。 To make them orderable, convert them to datetime objects first. 为了使它们可排序,请首先将它们转换为datetime对象。

>>> from datetime import datetime
>>> def parsedate(s):
...     return datetime.strptime(s, '%M-%d-%Y')
>>> keys = [parsedate(x[0]) for x in A]

Then you can do binary searches to cheaply (in O(lg n) time) do range searches: 然后,您可以执行二进制搜索以廉价地(在O(lg n)时间内)进行范围搜索:

>>> from bisect import bisect_left, bisect_right
>>> left = bisect_left(keys, parsedate('05-22-2013'))
>>> right = bisect_right(keys, parsedate('05-24-2013'))

Now A[left:right] is the range you're looking for. 现在, A[left:right]是您要查找的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM