如何从Scala列表中删除每个第n个元素? [英] how to remove every nth element from the Scala list?

查看:78
本文介绍了如何从Scala列表中删除每个第n个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以从 Scala List 中删除每个 n th 元素?

Is there any methods available to remove every n th element from the Scala List?

我希望我们可以在 filter 方法中执行此操作,并通过编写逻辑返回另一个列表.但这是一种有效的方法吗?

I hope we can do this inside filter method and return another list by writing a logic. But is this efficient way to do it?

推荐答案

简单:

list.zipWithIndex
    .filter { case (_, i) => (i + 1) % n != 0 }
    .map { case (e, _) => e }

这篇关于如何从Scala列表中删除每个第n个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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