返回不在现有列表中的数字范围内的整数值列表: [英] Return a list of Integer values that is not within a range of numbers in an existing list:

查看:147
本文介绍了返回不在现有列表中的数字范围内的整数值列表:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值列表:

  [0,7,4,5,3,1,4,5 ,5,1,7,0,7,7,0] 

并且想要返回任何值不在 [1..8]



范围内(即我想返回(从上面的例子)元素2,6和8的形式

  [2,6,8] 



一个函数。我知道 notElem 在这里可以很好地工作,但我不确定如何将列表[1..8]应用到上面显示的值列表中以仅显示元素然后。

解决方案使用过滤器来保留满足条件的元素。

  Prelude>过滤器(`notElem` theBigListOfValues)[1..8] 
[2,6,8]

或者只需(\\)运算符来完成nofollow> complement

 前奏> import Data.List 
Prelude Data.List> [1..8] \\ theBigListOfValues
[2,6,8]


I have a list of values:

[0,7,4,5,3,1,4,5,5,1,7,0,7,7,0]

and would like to return any values that are not in the range of [1..8]

(i.e. I would like to return (from the above example) the elements 2, 6 and 8 in the form

[2,6,8]

)

I seem to have trouble putting this together into a function. I know that notElem would work well here but am not sure on how to apply the list [1..8] to the list of values shown above to get the elements shown just then.

解决方案

Use filter to keep elements that satisfies a condition.

Prelude> filter (`notElem` theBigListOfValues) [1..8]
[2,6,8]

Or just take the complement using the (\\) operator.

Prelude> import Data.List
Prelude Data.List> [1..8] \\ theBigListOfValues
[2,6,8]

这篇关于返回不在现有列表中的数字范围内的整数值列表:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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