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

标签 python list loops integer sublist

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

  • (当前/焦点值小于或等于前一个)和(当前值等于下一个),即 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 的循环之外。 ,这些绞杀者叫什么?有没有办法不这样做而循环?

最佳答案

  1. 在您的示例中,第一个 if 永远不会被触发。但它将使用以下示例数据:

    x = (5 ,4, 4, 2)
    y = [[5, 4], [4, 2]]
    
  2. 更好是相当主观的。但不使用 Windows,只需改变值就可以达到相同的目标

    def func2(x):
        x = iter(x)
        try:
            prev_value = next(x)
            focal_value = next(x)
        except StopIteration:
            return [list(x)]
        sub_result = [prev_value]
        result = [sub_result]
        for next_value in x:
            if prev_value >= focal_value == next_value:
                sub_result.append(focal_value)
                sub_result = []
                result.append(sub_result)
            elif prev_value > focal_value < next_value:
                sub_result = [focal_value]
                result.append(sub_result)
            else:
                sub_result.append(focal_value)
            prev_value, focal_value = focal_value, next_value
        sub_result.append(focal_value)
        return result
    

    timeit 说它快了两倍以上

  3. 一旦保存了循环中的最后一个值,就必须在循环后对其进行特殊处理。但我的代码显示可以在循环内附加 sub_result 列表。

关于python - 根据值和规则拆分整数列表的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55804933/

相关文章:

python - 重复整数列表

c - 如何打印所有这些 block ?

python - Google 和 Oauthlib - 范围已更改

python - 重复 numpy 值并指定 dtype

python - 将 Raspberry Pi 数据发送到远程服务器

c# - 如何修复 List<List<string>> 中的数据

c - 重新排序文件和文件夹

javascript - 如何在express JS中正确显示动态页面?

c# - 简单的二进制到十进制转换器 - C# 中的问题

python - 防止 SQLAlchemy 自动设置 IDENTITY_INSERT