简体   繁体   English

Python:创建一个有序数字范围,跳过每Nth到Nth + D个数字的倒数

[英]Python: Create a range of ordered numbers skipping the inverse of every Nth through Nth+D number

Greetings stackoverflow friends. 问候stackoverflow的朋友。 I've decided to get a little wild this evening and party with for loops to iterate through a list I have created. 我决定今天晚上变得有点疯狂,并参加for循环来遍历我创建的列表。

It appears the party has been pooped on, though, as the manner through which I would like to create a range is not readily apparent, neither through research nor playing around, and proving bothersome 不过,该党似乎受到了困扰,因为无论是通过研究还是在游戏中玩弄,都令人烦恼,我想要创建范围的方式并不明显

The Desire: I would like to create a range of numbers much in a similar way that a range is usually created... by specifying range(start, stop, step) but with the minor alteration that I may additionally specify a step 'sweep' value such that range performed more like range(start, stop, step:sweep) 需求:我想以与通常创建范围类似的方式来创建一个数字范围...通过指定范围(开始,停止,步进),但稍作改动,我可以另外指定一个步进的值,以使范围的执行效果更类似于范围(开始,停止,步进:扫描)

That is to say, if the glorious function above existed it could be used as following; 就是说,如果上面的光荣功能存在,它就可以按如下方式使用:

range(0,16,3:5)
# [0,3,4,5,8,9,10,13,14,15]

Another example! 另一个例子!

range(0,24,2:9)
# [0,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,20,21,22,23]

Yet another! 完后还有!

range(0,24,3:9)
# [0,3,4,5,6,7,8,9,12,13,14,15,16,17,18,21,22,23]

Last one. 最后一个。

swept_range(5,20,3,4)
# [7, 8, 11, 12, 15, 16, 19]

In English, I desire a simple way to create a range of ordered numbers holding on to every Nth through Nth + D number group where D is some positive number. 用英语,我希望有一种简单的方法来创建一个有序数的范围,该数保持在第N至Nth + D个数字组中,其中D是一些正数。

I've looked at slices to no avail. 我看了片都无济于事。

I know MATLAB can succinctly do this but wasn't sure this exists in Python - does anyone? 我知道MATLAB可以简洁地执行此操作,但不确定Python中是否存在-有人吗?

def yrange(st, sp, N, D):
    return [st] + [j for i in range(st,sp,D) for j in range(i+N,i+D+1) if j < sp]

print yrange(0, 16, 3, 5)
# [0, 3, 4, 5, 8, 9, 10, 13, 14, 15]
print yrange(0, 24, 2, 9)
# [0, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23]
print yrange(0, 24, 3, 9)
# [0, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 15, 16, 17, 18, 21, 22, 23]

How about this generator, using modular arithmetic: 如何使用模块化算法生成器:

def swept_range(start, stop, step=1, sweep=1):
    for i in range(start, stop):
        if not 0 < i % sweep < step:
            yield i

You could also use a list comprehension, if you need a sequence, rather than an iterator: 如果需要序列,也可以使用列表推导,而不是迭代器:

def swept_range(start, stop, step=1, sweep=1):
    return [i for i in range(start, stop) if not 0 < i % sweep < step]
def srange(start, stop, step=1, sweep=0):
    if sweep < 0 :
        raise Exception("sweep needs to be positive.")
    STEPPING = 0
    SWEEPING = 1
    state = STEPPING
    next = start
    res = []
    while next < stop:
             res.append(next)

             #ignores state if sweep is 0
             if state == STEPPING or sweep == 0 :
                        state = SWEEPING
                        next = next + step
             elif state == SWEEPING :
                        next = next + 1      
                        if next % sweep == 0:
                                 state = STEPPING
    return res

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

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