简体   繁体   English

Python:运行长度编码?

[英]Python: Run length encoding?

I am trying to understand run length encoding and I understand the idea but I'm not sure how to write it so that the output is as follows:我试图理解运行长度编码,我理解这个想法,但我不确定如何编写它,以便 output 如下:

Input:输入:

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

Output: Output:

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

Question: A list is run-length encoded by representing it as a list of pairs (2-tuples), where each pair is a number and the length of the "run" of that number, where the length is 1 if a number occurs once, 2 if it occurs twice in a row, etc. Write a function run_length_encode(nums) that returns the run-length encoded representation of the list of integers, nums.问题:通过将列表表示为对列表(2元组)来对列表进行游程长度编码,其中每对是一个数字和该数字的“游程”的长度,如果出现数字,则长度为1一次,如果连续出现两次,则为 2,依此类推。编写一个 function run_length_encode(nums),它返回整数列表的游程编码表示,nums。

Can someone explain to me how to do this and explain what each step is doing?有人可以向我解释如何做到这一点并解释每个步骤在做什么吗? Unfortunately I'm struggling to grasp some things in Python but I'm slowly getting it.不幸的是,我在努力掌握 Python 中的一些东西,但我正在慢慢理解它。

Thank you!谢谢!

The following code will do the trick, although it's not necessarily the most "Pythonic" way to do it:以下代码可以解决问题,尽管它不一定是最“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([]))

As expected, the output is:正如所料,output 是:

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

In a bit more detail, it sets up an output list containing a tuple representing the first input item.更详细地说,它设置了一个 output 列表,其中包含一个表示第一个输入项的元组。 So, for 5 , it makes a list [(5, 1)] (value 5 with count 1 ).因此,对于5 ,它会创建一个列表[(5, 1)] (值为5 ,计数为1 )。

Then it processes every other input item.然后它处理所有其他输入项。 If the item has the same value as the last one processed, it simply increases the count.如果该项目与最后一个处理的项目具有相同的值,它只是增加计数。

If it's a different value to the last one processed, it creates a new output value in the output list with the new value and count of one, similar to what was done for the initial input value.如果它与最后一个处理的值不同,它会在 output 列表中创建一个新的 output 值,新值和计数为 1,类似于对初始输入值所做的操作。

So, as you run through the items for your example, you'll see how the list changes:因此,当您浏览示例中的项目时,您将看到列表如何变化:

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.

The only other bit is detecting an empty input list before starting that process, so that you don't try to use a non-existent first value.唯一的另一位是在开始该过程之前检测空输入列表,这样您就不会尝试使用不存在的第一个值。

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

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