以任意倍数

时间:2017-06-23 10:47:50

标签: python arrays list list-comprehension

我有一个数组,它是一个时间列表(它是10年内的天数),我想删除该列表中的某个选项,然后根据剩余的天数执行计算。这是用于天体物理模拟,我试图选择某些“观察”另一个数据集的模式。

我的数组是:

t = [0,1,2,3,...,3650]

就我的目的而言,我希望每年保留前180天并删除剩下的剩余部分,例如:

t_new = [1,2,...,178,179,365,366,...]

我通过以下方式实现了这一目标:

a = 180
b = 365 - a

t_new = [x for i, x in enumerate(t) if i%(a+b) < a]

将a和b作为变量让我能够在每年保持不同数量的数据。

现在我要做的是保留每30个剩余数据中的前10个。这将模拟只能在每个月的前10天和每年的前6个月观察。

我试过了:

c = 10
d = 30-c

t_new2 = [x for i, x in enumerate(t_new) if i%(c+d) < c]

这应该导致:

t_new2 = [0,1,2,3,4,5,6,7,8,9,30,31,...,178,179,365,366,...]

所以这是我们每30天(每月)删除最后20天,以及365年(每年)的最后185天之后的剩余日子。

但是我收到了错误:

ValueError: too many values to unpack

有没有更好的做这个逻辑,也许将它组合成一行?

谢谢!

后续问题:

到目前为止的解决方案是,

t_new = [365*year+day for year in range(10) for day in range(180) if day%30 < 10]

根据每个索引的值编辑原始数组。我想在第二个数组中只保留完全相同的索引,其值实际上是随机的,有没有办法将上面的内容转换为索引操作?

3 个答案:

答案 0 :(得分:4)

  

注意:请注意,您在此处过度简化了日历。这里忽略闰年

您可以按照以下方式执行此操作:

[day for year in range(10) for day in range(365*year,365*year+180)]

代码的工作原理如下:首先我们迭代year s:从0(包括)到10(不包括)的范围,所以0,1,2,3,4,5,6,7 ,8和9。

接下来,我们每年都会day365*year(“虚拟年”开始的那一天)到365*year+180(我们180天结束的那一天)进行迭代。

对于每一天,我们将其添加到列表中。

这比更高效,而不是在列表理解中使用if过滤器语句:此处仅生成“有效”天。过滤器当然可以获得相同的功能,但您将首先生成一组(可能很大)的值,您必须稍后撤回。

  

现在我要做的是保留每30个剩余数据中的前10个。

我们可以通过在此处添加其他过滤器来实现此目的:

[365*year+day for year in range(10) for day in range(180) if day%30 < 10]

这将产生:

>>> [365*year+day for year in range(2) for day in range(180) if day%30 < 10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524]

(我仅在前两年制作了一份清单)

编辑:如果您想要上述索引的数组索引,那么您只需使用:

[a[365*year+day] for year in range(10) for day in range(180) if day%30 < 10]

其中a是包含值的原始列表。

答案 1 :(得分:0)

能够生成一组列表,其中包含重复较长时段的连续值集 如[1,2,3,11,12,13,21,22,23,...],我们需要提供4个参数

  • 连续部分start_no的起始编号
  • 连续元素数elem_count
  • 期间(数字)期间
  • 您考虑的总天数

示例代码

start_no = 1
elem_count = 3
period = 10
tot = 3650
t_new = [x for x in range (tot) if x%period<=elem_count and x%period>=start_no]
print(t_new)

答案 2 :(得分:0)

使用生成器来节省一些内存的替代方法,也许更加pythonic ......

""" This approach (using a function with yield statement) is called a generator
It allows to create series of data without reserving memory for the entire thing
only the current few parameters are stored in memory"""

def iterfunc(start_no, elem_count, period):
    """this is the generator"""
    i = start_no
    while 1:
        if i%period<=elem_count and i%period>=start_no :
            yield i
        i+=1

""" Need to create an instance of the generator,
so that this instance remembers our parameters,
also this way we can create more instances with 
different parameters in each one
"""
day = iterfunc(1, 3, 10)

""" Here we print the days, but equally well we can append each to a list """
for x in range(30):
    print(day.next())