Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Removing and filtering List elements</strong></p> <p>In Scala you can filter the list to remove elements.</p> <pre><code>scala&gt; val courses = List("Artificial Intelligence", "Programming Languages", "Compilers", "Networks", "Databases") courses: List[java.lang.String] = List(Artificial Intelligence, Programming Languages, Compilers, Networks, Databases) </code></pre> <p>Let's remove a couple of classes:</p> <pre><code>courses.filterNot(p =&gt; p == "Compilers" || p == "Databases") </code></pre> <p>You can also use remove but it's deprecated in favor of filter or filterNot.</p> <p>If you want to remove by an index you can associate each element in the list with an ordered index using <code>zipWithIndex</code>. So, <code>courses.zipWithIndex</code> becomes:</p> <p><code>List[(java.lang.String, Int)] = List((Artificial Intelligence,0), (Programming Languages,1), (Compilers,2), (Networks,3), (Databases,4))</code></p> <p>To remove the second element from this you can refer to index in the Tuple with <code>courses.filterNot(_._2 == 1)</code> which gives the list:</p> <p><code>res8: List[(java.lang.String, Int)] = List((Artificial Intelligence,0), (Compilers,2), (Networks,3), (Databases,4))</code></p> <p>Lastly, another tool is to use <code>indexWhere</code> to find the index of an arbitrary element. </p> <p><code>courses.indexWhere(_ contains "Languages") res9: Int = 1</code></p> <p><strong>Re your update</strong></p> <blockquote> <p>I'm writing a function to remove the corresponding element from each lists, and all I know is that 1) the indices correspond and 2) the user inputs the course name. How can I remove the corresponding element from each list using filterNot?</p> </blockquote> <p>Similar to Nikita's update you have to "merge" the elements of each list. So courses, meridiems, days, and times need to be put into a Tuple or class to hold the related elements. Then you can filter on an element of the Tuple or a field of the class.</p> <p>Combining corresponding elements into a Tuple looks as follows with this sample data:</p> <pre><code>val courses = List(Artificial Intelligence, Programming Languages, Compilers, Networks, Databases) val meridiems = List(am, pm, am, pm, am) val times = List(100, 1200, 0100, 0900, 0800) val days = List(MWF, TTH, MW, MWF, MTWTHF) </code></pre> <p>Combine them with zip: </p> <p><code>courses zip days zip times zip meridiems</code></p> <p><code>val zipped = List[(((java.lang.String, java.lang.String), java.lang.String), java.lang.String)] = List((((Artificial Intelligence,MWF),100),am), (((Programming Languages,TTH),1200),pm), (((Compilers,MW),0100),am), (((Networks,MWF),0900),pm), (((Databases,MTWTHF),0800),am))</code></p> <p>This abomination flattens the nested Tuples to a Tuple. There are better ways.</p> <p><code>zipped.map(x =&gt; (x._1._1._1, x._1._1._2, x._1._2, x._2)).toList</code></p> <p>A nice list of tuples to work with.</p> <p><code>List[(java.lang.String, java.lang.String, java.lang.String, java.lang.String)] = List((Artificial Intelligence,MWF,100,am), (Programming Languages,TTH,1200,pm), (Compilers,MW,0100,am), (Networks,MWF,0900,pm), (Databases,MTWTHF,0800,am))</code></p> <p>Finally we can filter based on course name using <code>filterNot</code>. e.g. <code>filterNot(_._1 == "Networks")</code></p> <p><code>List[(java.lang.String, java.lang.String, java.lang.String, java.lang.String)] = List((Artificial Intelligence,MWF,100,am), (Programming Languages,TTH,1200,pm), (Compilers,MW,0100,am), (Databases,MTWTHF,0800,am))</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload