将列表中的相邻元素分组 [英] Grouping adjacent elements in a list

查看:150
本文介绍了将列表中的相邻元素分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想写一个这样的函数:

input:[1,1,3,3,4,2,2,5,6 ,6]
输出:[[1,1],[3,3],[4],[2,2],[5],[6,6]]



它将相邻的元素进行分组。

此方法的名称应该是什么?是否有这个操作的标准名称?

解决方案

[1,1,3,3, 4,2,2,5,6,6] ,像 [1,1] 这样的东西通常被称为运行(如运行长度编码,请参阅Scala中的 RLE ) 。因此,我会调用方法 groupRuns


Let's say I want to write a function that does this:

input: [1,1,3,3,4,2,2,5,6,6] output: [[1,1],[3,3],[4],[2,2],[5],[6,6]]

It's grouping adjacent elements that are same.

What should the name of this method be? Is there a standard name for this operation?

解决方案

In [1,1,3,3,4,2,2,5,6,6], a thing like [1,1] is very often referred to as run (as in run-length encoding, see RLE in Scala). I'd therefore call the method groupRuns.

这篇关于将列表中的相邻元素分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆