Python 以任意倍数删除数组中的项

Python 以任意倍数删除数组中的项,python,arrays,list,list-comprehension,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

我有一个数组,它是一个时间列表(它是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]
因此,这是在我们删除了每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]
t_new=[365*年+范围内年份的天数(10)如果第%30天<10天,范围内的天数(180)]
它根据每个索引的值编辑原始数组。我想在第二个数组中只保留完全相同的索引,其值实际上是随机的,有没有办法将上面的内容转换为索引操作

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

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

[day for year in range(10) for day in range(365*year,365*year+180)]
代码的工作原理如下:首先,我们迭代
s:范围从0(包括)到10(不包括),因此0、1、2、3、4、5、6、7、8和9

接下来,每年我们都以
365*年
(虚拟年开始的那一天)迭代到
365*年+180
(180天结束的那一天)

对于每一个这样的日子,我们都会把它添加到列表中

这也比在列表理解中使用
if
filter语句更有效:这里只生成“有效”天数。过滤器当然可以获得相同的功能,但您将首先生成一组(可能较大)的值,以后必须提取这些值

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

我们可以通过在此处添加一个附加过滤器来实现这一点:

[365*year+day for year in range(10) for day in range(180) if day%30 < 10]
[365*年+范围内的年的天(10)范围内的天(180),如果天%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]
>>[365*年+范围内的年天数(2)范围内的天天数(180),如果天%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[365*年+日]范围内的年(10)范围内的天(180),如果天%30<10]

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

以便能够生成包含一组连续值的列表,这些值在更长的时间内重复 像[1,2,3,11,12,13,21,22,23,…],我们需要提供4个参数

  • 连续零件开始编号的开始编号
  • 连续元素数元素计数
  • 句号句号句号
  • <>你考虑tot/LI>的总天数
示例代码

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)
start\u no=1
元素计数=3
周期=10
tot=3650
t_new=[x表示范围内的x(tot),如果x%周期=开始编号]
打印(t_新)

另一种方法是使用生成器来节省一些内存,也许还有更多的Python功能

""" 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())
这种方法(使用带有yield语句的函数)称为生成器 它允许创建一系列数据,而无需为整个事件保留内存 只有当前少数参数存储在内存“”中 定义iterfunc(开始编号、元素计数、周期): “”“这是发电机”“” i=开始\u否 而1: 如果i%period=start\u no: 产量一 i+=1 “”“需要创建生成器的实例, 所以这个实例记住了我们的参数, 同样,通过这种方式,我们可以使用 每个参数中有不同的参数 """ 日=iterfunc(1,3,10) “我们在这里打印日期,但同样可以将每个日期附加到列表中” 对于范围(30)内的x: 打印(day.next())
闰年呢?是的,我现在忽略了这些:)一年365天,在你的列表中准确地输入。
对于我,在…
@MosesKoledoye谢谢,现在修复了你拥有的是
列表
s,而不是
数组
s。后者也存在,它们不是列表。非常感谢!这太棒了,它可以让我随意玩其他类型的观测计划:)是的,我现在忽略闰年,在我能走路之前我不想跑!接下来的问题,是否可以用数组的索引来写,这样我就可以从第二个数组中删除相同的元素,而不管它们的值是多少?@RichardHall:你能具体说明一下索引的含义吗。也许可以用一个例子来更新你的问题。我添加了第二个例子question@RichardHall:这回答了你的问题吗?我会在同一个答案中添加多个答案,而不是单独添加。
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)
""" 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())