根据值和规则拆分整数列表的更好方法

目标是根据以下规则根据每个元素的邻居拆分整数列表:


(当前/焦点值小于或等于上一个)和(当前值等于下一个),即 prev_value >= focal_value == next_value


(当前值小于上一个)和(当前值小于下一个),即 prev_value > focal_value < next_value


为了说明,给定x产品y:


x = (1, 4, 2, 1, 4, 2, 4, 1, 4, 1, 4, 4, 3)

y = [[1, 4, 2], [1, 4], [2, 4], [1, 4], [1, 4, 4, 3]]

assert func(x) == y

我试过了:


def per_window(sequence, n=1):

    """

    From http://stackoverflow.com/q/42220614/610569

        >>> list(per_window([1,2,3,4], n=2))

        [(1, 2), (2, 3), (3, 4)]

        >>> list(per_window([1,2,3,4], n=3))

        [(1, 2, 3), (2, 3, 4)]

    """

    start, stop = 0, n

    seq = list(sequence)

    while stop <= len(seq):

        yield tuple(seq[start:stop])

        start += 1

        stop += 1



def func(x):

    result = []

    sub_result = [x[0]]

    for prev_value, focal_value, next_value in per_window(x, 3):

        # These if and elif cases trigger syllable break.

        if prev_value >= focal_value == next_value:

            sub_result.append(focal_value)

            result.append(sub_result)

            sub_result = []

        elif prev_value > focal_value < next_value:

            result.append(sub_result)

            sub_result = []

            sub_result.append(focal_value)

        else: # no  break

            sub_result.append(focal_value)

    sub_result.append(next_value)

    result.append(sub_result)

    return result



x = (1, 4, 2, 1, 4, 2, 4, 1, 4, 1, 4, 4, 3)

y = [[1, 4, 2], [1, 4], [2, 4], [1, 4], [1, 4, 4, 3]]


assert func(x) == y

但我的问题是:

  • 如果我们仔细查看 if 和 elif “案例”,看起来第一个 if 永远不会被捕获,因为第二个 if 将首先到达。是对的吗?if第一个案例会出现的例子有哪些?

  • 有没有更好的方法来实现基于规则拆分子列表的相同目标?

  • 最后两个追加需要存在于循环之外per_window,这些扼杀者叫什么?有没有办法不这样做循环?


慕森王
浏览 147回答 1
1回答

白板的微信

在你的例子中,第一个 if 永远不会被触发。但这将与此示例数据一起使用:x = (5 ,4, 4, 2)y = [[5, 4], [4, 2]]更好是相当主观的。但是不使用窗口也可以达到相同的目标,只需改变值def func2(x):&nbsp; &nbsp; x = iter(x)&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; prev_value = next(x)&nbsp; &nbsp; &nbsp; &nbsp; focal_value = next(x)&nbsp; &nbsp; except StopIteration:&nbsp; &nbsp; &nbsp; &nbsp; return [list(x)]&nbsp; &nbsp; sub_result = [prev_value]&nbsp; &nbsp; result = [sub_result]&nbsp; &nbsp; for next_value in x:&nbsp; &nbsp; &nbsp; &nbsp; if prev_value >= focal_value == next_value:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub_result.append(focal_value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub_result = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append(sub_result)&nbsp; &nbsp; &nbsp; &nbsp; elif prev_value > focal_value < next_value:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub_result = [focal_value]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append(sub_result)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub_result.append(focal_value)&nbsp; &nbsp; &nbsp; &nbsp; prev_value, focal_value = focal_value, next_value&nbsp; &nbsp; sub_result.append(focal_value)&nbsp; &nbsp; return resulttimeit 说它快两倍以上一旦你持有循环中的最后一个值,你就必须在循环之后为它添加特殊处理。但是我的代码显示可以将sub_result列表附加到循环中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python