【问题标题】:how to check and rearrange Python's list which contains a series of incremental values [duplicate]如何检查和重新排列包含一系列增量值的 Python 列表 [重复]
【发布时间】:2021-11-05 17:04:40
【问题描述】:

我只是想知道如何将列表重新排列为具有一系列增量整数作为列表元素的列表列表。例如,假设我有一个如下列表:

example = [2, 43, 44, 64, 143, 144, 145, 146, 147, 148, 178, 179, 180, 181, 182, 183, 184, 211]

我想将此列表转换为如下列表:

converted_example = [[2], [43, 44], [64], [143, 144, 145, 146, 147, 148], [178, 179, 180, 181, 182, 183, 184], [211]]

您对此有什么想法吗?提前致谢!

【问题讨论】:

标签: python algorithm sorting


【解决方案1】:
import itertools


def groupby_adjacent(max_step_size=1):
    # generate a persistent function that tracks successive calls, 
    # to feed to itertools.groupby
    # (assume the input is already sorted)
    v = {
        'last': None,   # last value encountered
        'group': 0      # value to return for groupby
    }
    def f(current):
        # if the current element exceeds the last element by more than one,
        # break the groupby and start a new group
        if v['last'] is not None and current - max_step_size > v['last']:
            v['group'] = v['group'] + 1
        # otherwise, just keep the same group as we currently have
        v['last'] = current
        return v['group']
    return f

example = [2, 43, 44, 64, 143, 144, 145, 146, 147, 148, 178, 179, 180, 181, 182, 183, 184, 211]

converted_example = [list(tup[1]) for tup in 
                         itertools.groupby(sorted(example), key=groupby_adjacent())
                    ]
# [[2], [43, 44], [64], [143, 144, 145, 146, 147, 148], [178, 179, 180, 181, 182, 183, 184], [211]]

groupby_adjacent() 的每次调用都会产生一个新版本的f,它有自己独特的局部变量集,然后可以将返回的f 传递给单个.groupby() 函数。我使用 dict 来存储变量而不是实际使用变量,因为我想避免使用 global 关键字(它的行为可能超出您的预期)。

如果您能保证example 已经被排序,当然可以省略对sorted() 的调用。

【讨论】:

  • nonlocal 怎么样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 2018-06-20
  • 1970-01-01
  • 2019-07-12
  • 2020-08-02
  • 2020-02-18
相关资源
最近更新 更多