Scala : Map的过滤

       问题:过滤映射中的元素,可以直接修改可变映射,也可以在不可变映射中应用过滤算法获得一个新的映射

       解决方案:Use the retain method to define the elements to retain when using a mutable map, and use filterKeys or filter to filter the elements in a mutable or immutable map, remembering to assign the result to a new variable.

       You can filter the elements in a mutable Map  using the retain method to specify which elements should be retained.

scala> var x = collection.mutable.Map(1 -> "a", 2 -> "b", 3 -> "c")
x: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)

scala> x.retain((k,v) => k > 1)
res0: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 3 -> c)

scala> x
res1: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 3 -> c)

        As shown, retain modifies a mutable map in place. As implied by the anonymous function signature used in that example:

(k,v) => ...
     

        Your algorithm can test both the key and value of each element to decide which elements to retain in the map.

        In a related note, the transform method doesn’t filter a map, but it lets you transform the elements in a mutable map:


scala> x.transform((k,v) => v.toUpperCase)
res0: scala.collection.mutable.Map[Int,String] = Map(2 -> B, 3 -> C)

scala> x
res1: scala.collection.mutable.Map[Int,String] = Map(2 -> B, 3 -> C)

     Depending on your definition of “filter,” you can also remove elements from a map using methods like remove and clear.

    

Mutable and immutable maps

      When working with a mutable or immutable map, you can use a predicate with the  filterKeys methods to define which map elements to retain. When using this method,  remember to assign the filtered result to a new variable:

scala> val x = Map(1 -> "a", 2 -> "b", 3 -> "c")
x: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)

scala> val y = x.filterKeys(_ > 2)
y: scala.collection.Map[Int,String] = Map(3 -> c)
   

   

       The predicate you supply should return true for the elements you want to keep in the new collection and false for the elements you don’t want.

        If your algorithm is longer, you can define a function (or method), and then use it in the filterKeys call, rather than using an anonymous function. First define your method, such as this method, which returns true when the value the method is given is 1:

      
scala> def only1(i: Int) = if (i == 1) true else false
only1: (i: Int)Boolean

      

         Then pass the method to the filterKeys method:


scala> val x = Map(1 -> "a", 2 -> "b", 3 -> "c")
x: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)

scala> val y = x.filterKeys(only1)
y: scala.collection.Map[Int,String] = Map(1 -> a)

       

          In an interesting use, you can also use a  Set with filterKeys to define the elements to retain:


scala> var m = Map(1 -> "a", 2 -> "b", 3 -> "c")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)

scala> val newMap = m.filterKeys(Set(2,3))
newMap: scala.collection.immutable.Map[Int,String] = Map(2 -> b, 3 -> c)


             For instance, the Map version of the filter method lets you filter the map elements by either key, value, or both. The filter method provides your predicate a Tuple2, so you can access the key and value as shown in these examples:

            

scala> var m = Map(1 -> "a", 2 -> "b", 3 -> "c")
m: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c)

// access the key
scala> m.filter((t) => t._1 > 1)
res0: scala.collection.immutable.Map[Int,String] = Map(2 -> b, 3 -> c)

// access the value
scala> m.filter((t) => t._2 == "c")
res1: scala.collection.immutable.Map[Int,String] = Map(3 -> c)

            可以自定义一个方法fx(),将Tuple2传过去

var xxx = x.filter{case(k,v)=>fx(k,v)}


           The take method lets you “take” (keep) the first N elements from the map:

scala> m.take(2)
res2: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的使用 Scala 实现的协同过滤推荐算法的程序示例: ```scala import org.apache.spark.{SparkConf, SparkContext} object CollaborativeFiltering { def main(args: Array[String]): Unit = { // 创建 SparkConf 对象并设置应用程序的名称 val conf = new SparkConf().setAppName("Collaborative Filtering") // 创建 SparkContext 对象 val sc = new SparkContext(conf) // 加载评分数据 val ratings = sc.textFile("path/to/ratings.csv") .map(line => { val fields = line.split(",") (fields(0).toInt, fields(1).toInt, fields(2).toDouble) }) // 构建用户-物品评分矩阵 val userItemMatrix = ratings.map { case (user, item, rating) => (user, (item, rating)) }.groupByKey() // 计算物品之间的相似度 val itemSimilarities = userItemMatrix.flatMap { case (_, items) => for { (item1, rating1) <- items (item2, rating2) <- items if item1 != item2 } yield ((item1, item2), rating1 * rating2) }.reduceByKey(_ + _) // 获取某个物品的相似物品列表 def getSimilarItems(itemId: Int, n: Int): Array[(Int, Double)] = { itemSimilarities.filter { case ((item1, _), _) => item1 == itemId }.map { case ((_, item2), similarity) => (item2, similarity) }.top(n)(Ordering.by(_._2)) } // 示例:获取物品 1 的相似物品列表 val similarItems = getSimilarItems(1, 5) similarItems.foreach { case (item, similarity) => println(s"Item $item: Similarity $similarity") } // 停止 SparkContext 对象 sc.stop() } } ``` 上述代码使用 Spark 编写了一个协同过滤推荐算法的程序。它假设评分数据以 CSV 格式存储,并且每行包含三个字段:用户ID、物品ID和评分值。程序首先加载评分数据,然后构建用户-物品评分矩阵。接着计算物品之间的相似度,并提供一个函数用于获取某个物品的相似物品列表。最后,示例展示了获取物品 1 的相似物品列表的过程。 请注意,上述代码是基于 Apache Spark 编写的,并假设已在项目添加了 Spark 相关的依赖。如果要运行此程序,需要将代码的文件路径替换为实际的评分数据文件路径,并确保 Spark 相关的配置正确设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值