简体   繁体   English

如何从列表中选择数字序列?

[英]How to pick a sequence of numbers from a list?

I have a startnumber and an endnumber. 我有一个开始编号和一个结束编号。
From these numbers I need to pick a sequence of numbers. 从这些数字中,我需要选择一个数字序列。
The sequences is not always the same. 顺序并不总是相同的。

Example: 例:

startnumber = 1
endnumber = 32

I need to create a list of numbers with a certain sequence 我需要创建具有特定顺序的数字列表
pe pe
3 numbers yes, 2 numbers no, 3 numbers yes, 2 numbers no.. etc 3个数字,2个数字,3个数字,2个数字..等

Expected output: 预期产量:

[[1-3],[6-8],[11-13],[16-18],[21-23],[26-28],[31-32]]

(at the end there are only 2 numbers remaining (31 and 32)) (最后只剩下两个数字(31和32))

Is there a simple way in python to select sequences of line from a range of numbers? python中有没有一种简单的方法可以从一系列数字中选择行序列?

numbers = range(1,33)
take = 3
skip = 2
seq = [list(numbers[idx:idx+take]) for idx in range(0, len(numbers),take+skip)]

Extrapolating this out: 推断出来:

def get_data(data, filterfunc=None):
    if filterfunc is None:
        filterfunc = lambda: True  # take every line

    result = []
    sub_ = []
    for line in data:
        if filterfunc():
            sub_.append(line)
        else:
            if sub_:
                result.append(sub_)
                sub_ = []

    return result

# Example filterfunc
def example_filter(take=1, leave=1):
    """example_filter is a less-fancy version of itertools.cycle"""

    while True:
        for _ in range(take):
            yield True
        for _ in range(leave):
            yield False

# Your example
final = get_data(range(1, 33), example_filter(take=3, leave=2))

As alluded to in the docstring of example_filter, the filterfunc for get_data is really just expecting a True or False based on a call. 正如example_filter的文档字符串中提到的filterfunc用于get_data的filterfunc实际上只是期望基于调用的TrueFalse You could change this easily to be of the signature: 您可以轻松地将其更改为签名:

def filterfunc(some_data: object) -> bool:

So that you can determine whether to take or leave based on the value (or even the index), but it currently takes no arguments and just functions as a less magic itertools.cycle (since it should return its value on call, not on iteration) 这样您就可以基于值(甚至是索引)来确定是否接受或离开,但是它目前不接受任何参数,而只是充当一个不太魔术的itertools.cycle (因为它应该在调用时返回其值,而不是在迭代时返回) )

from itertools import islice
def grouper(iterable, n, min_chunk=1):
    it = iter(iterable)
    while True:
       chunk = list(islice(it, n))
       if len(chunk) < min_chunk:
           return
       yield chunk

def pick_skip_seq(seq, pick, skip, skip_first=False):
    if skip_first:
        ret = [ x[skip:] for x in grouper(seq, pick+skip, skip+1) ]
    else:
        ret = [ x[:pick] for x in grouper(seq, pick+skip) ]
    return ret

pick_skip_seq(range(1,33), 3, 2) gives required list. pick_skip_seq(range(1,33), 3, 2)给出所需的列表。

In pick_skip_seq(seq, pick, skip, skip_first=False) , seq is sequence to pick/skip from, pick / skip are no. pick_skip_seq(seq, pick, skip, skip_first=False)seq是从中进行拾取/跳过的顺序,而pick / skip是no。 of elements to pick/skip, skip_first is to be set True if such behavior is desired. 元素的选择/跳过,如果需要这种行为,将skip_first设置为True

grouper returns chunks of n elements, it ignores last group if it has less than min_chunk elements. grouper返回n个元素的块,如果少于min_chunk个元素,它将忽略最后一个组。 It is derived from stuff given in https://stackoverflow.com/a/8991553/1921546 . 它源自https://stackoverflow.com/a/8991553/1921546中提供的内容

Demo: 演示:

# pick 3 skip 2 
for i in range(30,35):
    print(pick_skip_seq(range(1,i), 3, 2))

# skip 3 pick 2 
for i in range(30,35):
    print(pick_skip_seq(range(1,i), 3, 2, True))

An alternative implementation of pick_skip_seq : pick_skip_seq的替代实现:

from itertools import chain,cycle,repeat,compress
def pick_skip_seq(seq, pick, skip, skip_first=False):
    if skip_first:
        c = cycle(chain(repeat(0, skip), repeat(1, pick)))
    else:
        c = cycle(chain(repeat(1, pick), repeat(0, skip)))
    return list(grouper(compress(seq, c), pick))

All things used are documented here: https://docs.python.org/3/library/itertools.html#itertools.compress 此处记录了所有使用的东西: https : //docs.python.org/3/library/itertools.html#itertools.compress

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

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