繁体   English   中英

Python:运行长度编码?

[英]Python: Run length encoding?

我试图理解运行长度编码,我理解这个想法,但我不确定如何编写它,以便 output 如下:

输入:

data = [5, 5, 5, 10, 10]

Output:

[(5, 3), (10, 2)]

问题:通过将列表表示为对列表(2元组)来对列表进行游程长度编码,其中每对是一个数字和该数字的“游程”的长度,如果出现数字,则长度为1一次,如果连续出现两次,则为 2,依此类推。编写一个 function run_length_encode(nums),它返回整数列表的游程编码表示,nums。

有人可以向我解释如何做到这一点并解释每个步骤在做什么吗? 不幸的是,我在努力掌握 Python 中的一些东西,但我正在慢慢理解它。

谢谢!

以下代码可以解决问题,尽管它不一定是最“Pythonic”的方法:

def rle_encode(in_list):
    # Handle empty list first.

    if not in_list:
        return []

    # Init output list so that first element reflect first input item.

    out_list = [(in_list[0], 1)]

    # Then process all other items in sequence.

    for item in in_list[1:]:
        # If same as last, up count, otherwise new element with count 1.

        if item == out_list[-1][0]:
            out_list[-1] = (item, out_list[-1][1] + 1)
        else:
            out_list.append((item, 1))

    return out_list

print(rle_encode([5, 5, 5, 10, 10]))
print(rle_encode([5, 5, 5, 10, 10, 7, 7, 7, 5, 10, 7]))
print(rle_encode([]))

正如所料,output 是:

[(5, 3), (10, 2)]
[(5, 3), (10, 2), (7, 3), (5, 1), (10, 1), (7, 1)]
[]

更详细地说,它设置了一个 output 列表,其中包含一个表示第一个输入项的元组。 因此,对于5 ,它会创建一个列表[(5, 1)] (值为5 ,计数为1 )。

然后它处理所有其他输入项。 如果该项目与最后一个处理的项目具有相同的值,它只是增加计数。

如果它与最后一个处理的值不同,它会在 output 列表中创建一个新的 output 值,新值和计数为 1,类似于对初始输入值所做的操作。

因此,当您浏览示例中的项目时,您将看到列表如何变化:

Input     Output              Description
-----     ------              -----------
  5       [(5, 1)]            First value, init with count 1.
  5       [(5, 2)]            Same as last, increase count.
  5       [(5, 3)]            Same as last, increase count.
 10       [(5, 3), (10, 1)]   New value, append with count 1.
 10       [(5, 3), (10, 2)]   Same as last, increase count.

唯一的另一位是在开始该过程之前检测空输入列表,这样您就不会尝试使用不存在的第一个值。

暂无
暂无

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

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