how to remove sub list

后端 未结 3 1994
别那么骄傲
别那么骄傲 2021-01-21 00:22

How can I remove all occurrences of a sublist from a list, eg

List(1, 2, 3, 4, 5, 6, 7, 4, 8, 9, 10, 5).removeSubList(4, 5)

should remove all o

相关标签:
3条回答
  • 2021-01-21 00:41

    A recursive solution using indexOfSlice:

    def removeSubList(l: List[Int], sublist: List[Int]): List[Int] = l.indexOfSlice(sublist) match {
      case -1 => l
      case index => removeSubList(l.patch(index, Nil, sublist.length), sublist)
    }
    
    // all of these print List(1 ,2 ,3):
    println(removeSubList(List(1,2,3), List(4,5)))
    println(removeSubList(List(1,2,3,4,5), List(4,5)))
    println(removeSubList(List(4,5,1,2,3), List(4,5)))
    println(removeSubList(List(4,5,1,2,4,5,3), List(4,5)))
    

    EDITED:

    • (thanks @corvus_192) reverting to using indexOfSlice version instead of using diff, which ignores sublist order.
    • (thanks @The Archetypal Paul) using patch for cleaner removal of sublist
    0 讨论(0)
  • 2021-01-21 00:44

    Using Tzach Zohar idea with different implementation:

    def removeSubList[T](list: List[T], sublist: List[T]): List[T] =
        if (list.containsSlice(sublist)) 
            removeSubList(list.diff(sublist), sublist)
        else list
    
    0 讨论(0)
  • 2021-01-21 00:45

    Variant on Tzach Zohar idea but with the assumption the OP wants a result that does not include the given sublist ever. Handles the case when sublist is empty

      /**
        * Return `s` with no occurrence of `target` present
        *
        * @param s
        * @param target
        * @return The sequence free of any occurrence of the target slice
        */
      def removeCompletely[E](s: Seq[E], target: Seq[E]): Seq[E] = {
        if (s.isEmpty || target.isEmpty)
          s
        else
          removeCompletely0(s, target)
      }
    
      private def removeCompletely0[E](s: Seq[E], target: Seq[E]): Seq[E] = {
        val sliceIdx = s.indexOfSlice(target)
        if (sliceIdx >= 0) {
          val patched = s.patch(sliceIdx, s take 0, target.size)
          removeCompletely0(patched, target)
        }
        else {
          s
        }
      }
    
    0 讨论(0)
提交回复
热议问题