python - 分割大列表的最快方法

标签 python list date datetime filter

我有 DOY 数据列表。该列表几乎有 900,000 个条目。我遇到的问题是数据最初上升到 365,但当我希望它超过 365 时又从 1 开始。例如:

>>> continue_list([140, 141, 145, 270, 365, 365.90, 1, 2, 5, 360])
>>> [140, 141, 145, 270, 365, 365.90, 366, 367, 370, 725] 

这是我的尝试:

def continue_list(x):
    l = []
    m = []
    for i in x:
        if math.floor(i) == 1:
            l = l+ x[x.index(i):]
            l = list(map(lambda x: x+365, l))
            m = m + x[:x.index(i)]
            new_x = m + l
    return new_x

这非常适合小型列表。但对于一个有 900,000 个条目的列表来说,这是不可行的;我的电脑立即崩溃。关于更快的方法有什么建议吗?

最佳答案

我会推荐一个生成器:

l = [140, 141, 145, 270, 365, 365.90, 1, 2, 5, 360, 1]

def wrap_at_365(it):
    # The number of wraps that have been found.
    wraps = 0
    it = iter(it)
    # Get the first item
    last = next(it)
    yield last
    for item in it:
        # Identify a wrap by checking if the next item is smaller than the previous
        if item < last:
            wraps += 1
        yield item + 365 * wraps
        last = item

它可以立即转换为列表:

>>> list(wrap_at_365(l))
[140, 141, 145, 270, 365, 365.9, 366, 367, 370, 725, 731]

在我的计算机上,转换包含 100 万个数字的列表大约需要 0.5 秒。


不过识别次数需要小心。我检查了最后一项比当前项大,但是如果第一年它是 100 而下一项代表第二年 110 该怎么办?这不会被发现,而且我实际上不知道如何检测到它。您的数据包含更多详细信息吗?那么闰年呢?

关于python - 分割大列表的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42492071/

相关文章:

python - 在 Python 中通过引用将一个列表的元素存储在另一个列表中?

java - 检查日历日期是否为星期日

ruby - 将字符串转换为日期时间

python - 如何在不在 Python 中创建中间列表的情况下拆分字符串并重新加入它?

python - 如何在 AJAX 调用中使用 django-debug-toolbar?

python - 使用分组数据 reshape Pandas Dataframe(从长到宽)

python - Unicodedata.normalize : TypeError: normalize() argument 2 must be str, 未列出

java - 声明一个具有多种类型的 ArrayList

linux - bash date,在 n 小时后的特定时间戳上创建一个新对象

python - Durand-kerner 实现不起作用