python - 在 python 列表中保持日期顺序的同时插入缺失的日期

标签 python datetime numpy

我有一个包含 [yyyy, value] 项目的列表列表,每个子列表按增加的年份排序。这是一个示例:

A = [
    [[2008, 5], [2009, 5], [2010, 2], [2011, 5], [2013, 17]], 
    [[2008, 6], [2009, 3], [2011, 1], [2013, 6]], [[2013, 9]], 
    [[2008, 4], [2011, 1], [2013, 4]], 
    [[2010, 3], [2011, 3], [2013, 1]], 
    [[2008, 2], [2011, 4], [2013, 1]], 
    [[2009, 1], [2010, 1], [2011, 3], [2013, 3]], 
    [[2010, 1], [2011, 1], [2013, 5]], 
    [[2011, 1], [2013, 4]], 
    [[2009, 1], [2013, 4]], 
    [[2008, 1], [2013, 3]], 
    [[2009, 1], [2013, 2]], 
    [[2013, 2]], 
    [[2011, 1], [2013, 1]],
    [[2013, 1]], 
    [[2013, 1]], 
    [[2011, 1]], 
    [[2011, 1]]
    ]

我需要的是在 min(year) 和 max(year) 之间插入所有缺失的年份,并确保保留顺序。因此,例如,采用 A 的第一个子列表:

[2008, 5], [2009, 5], [2010, 2], [2011, 5], [2013, 17]

应该看起来像:

[min_year, 0]...[2008, 5], [2009, 5], [2010, 2], [2011, 5], [2012, 0],[2013, 17],..[max_year, 0]

此外,如果任何子列表仅包含一个项目,则应对其应用相同的过程,以便原始值保留其假定的顺序,并正确插入其余的最小到最大(年,值)项目。

有什么想法吗?

谢谢。

最佳答案

minyear = 2008
maxyear = 2013
new_a = []
for group in A:
    group = group
    years = [point[0] for point in group]
    print years
    for year in range(minyear,maxyear+1):
        if year not in years:
            group.append([year,0])
    new_a.append(sorted(group))
print new_a

这会产生:

[   [[2008, 5], [2009, 5], [2010, 2], [2011, 5], [2012, 0], [2013, 17]],
    [[2008, 6], [2009, 3], [2010, 0], [2011, 1], [2012, 0], [2013, 6]],
    [[2008, 0], [2009, 0], [2010, 0], [2011, 0], [2012, 0], [2013, 9]],
    [[2008, 4], [2009, 0], [2010, 0], [2011, 1], [2012, 0], [2013, 4]],
    [[2008, 0], [2009, 0], [2010, 3], [2011, 3], [2012, 0], [2013, 1]],
    [[2008, 2], [2009, 0], [2010, 0], [2011, 4], [2012, 0], [2013, 1]],
    [[2008, 0], [2009, 1], [2010, 1], [2011, 3], [2012, 0], [2013, 3]],
    [[2008, 0], [2009, 0], [2010, 1], [2011, 1], [2012, 0], [2013, 5]],
    [[2008, 0], [2009, 0], [2010, 0], [2011, 1], [2012, 0], [2013, 4]],
    [[2008, 0], [2009, 1], [2010, 0], [2011, 0], [2012, 0], [2013, 4]],
    [[2008, 1], [2009, 0], [2010, 0], [2011, 0], [2012, 0], [2013, 3]],
    [[2008, 0], [2009, 1], [2010, 0], [2011, 0], [2012, 0], [2013, 2]],
    [[2008, 0], [2009, 0], [2010, 0], [2011, 0], [2012, 0], [2013, 2]],
    [[2008, 0], [2009, 0], [2010, 0], [2011, 1], [2012, 0], [2013, 1]],
    [[2008, 0], [2009, 0], [2010, 0], [2011, 0], [2012, 0], [2013, 1]],
    [[2008, 0], [2009, 0], [2010, 0], [2011, 0], [2012, 0], [2013, 1]],
    [[2008, 0], [2009, 0], [2010, 0], [2011, 1], [2012, 0], [2013, 0]],
    [[2008, 0], [2009, 0], [2010, 0], [2011, 1], [2012, 0], [2013, 0]]]

关于python - 在 python 列表中保持日期顺序的同时插入缺失的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17865549/

相关文章:

python - 使用 Django 登录 Twitter 的最佳方式

python - SQLAlchemy 中的 PostgreSQL 多维数组,不确定语法

python - 如何使用 argparse 将列表作为命令行参数传递?

python - 根据日期和错误格式过滤 pandas 数据框

linux - touch utility 设置相对时间

python - 在 python 中使用 numpy 对 100x100 数组进行排序

python - Numpy 使用多维度索引

python - 如何在 Python 的二维数组中查找值的索引?

python - 无法在 python selenium 中加载 firefox 附加组件

java - 如果我有一天的特定日期,我如何获得前一周的那一天的日期?