score:4
you can calculate the average of the second elements of the list of tuples, you don't need to do the sum yourself because scala has a builtin function for that. first we need to transform the list of tuples to a list of int
values, we can do that using the map
function as shown below
val average = list.map(_._2).sum/list.size.todouble
now you have the average, you can filter your list based on its value
val newlist = list.filter(_._2 < average)
note that we didn't remove anything from the list, we created a new one with filtered elements
score:1
you can do:
val sum = list.map(_._2).sum
val avg: double = sum / list.size.todouble
val filtered = list.filter(_._2 > avg)
note this is traversing the list twice, once for summing and once for filtering. another thing to note is that scala list[t]
is immutable. when you filter, you're creating a new list
object with the filtered data.
score:1
val average = list.map(_._2).sum / list.size.todouble
list.filter(p => p._2 >= average)
you need to cast to double
, else average would be cast to an int
and be imprecise. the filter
only keeps the element greater than the average.
Source: stackoverflow.com
Related Query
- Calculate average and remove from list in Scala
- How to remove an item from a list in Scala having only its index?
- Get head item and tail items from scala list
- How to remove 2 or more duplicates from list and maintain their initial order?
- How can I idiomatically "remove" a single element from a list in Scala and close the gap?
- Scala Spark : How to create a RDD from a list of string and convert to DataFrame
- remove elements from a List of case class structure efficiently and elegantly
- Remove one element from Scala List
- Converting a Heterogeneous List to and from Json in Play for Scala
- Scala remove elements from list of tuples
- How can I remove duplicates from a list in Scala with pattern matching?
- How to remove quotes from front and end of the string Scala
- need to remove first line from a string and then first two word in scala
- Remove zero rows from a list of list in Scala
- calculate average from values in map in scala
- Remove element from List of Maps in Scala
- scala list counting element and find indices from other list
- Number of Cycles from list of values, which are mix of positives and negatives in Spark and Scala
- How to calculate the ApproxQuanitiles from list of Integers into Spark DataFrame column using scala
- Deleting an element from a List in Scala using a loop and only a loop
- how can I write a function in scala called Order that takes one arguement: a list of Ints. And returns the same List of Ints from least to greatest
- Scala Netty ByteToMessageDecoder subclass remove bytes from input and pass to next pipeline level
- How to remove specific element from list, and group list elements using scala?
- Scala remove a single object from a list
- Scala getting the adjacency list of each vertex from a list of source node and destination node
- Read Nested List from Database with relation and Create another List using Scala Recursivly
- remove elements from a List of case class structure when found duplicate scala using foldleft
- Scala - create a new list and update particular element from existing list
- Get and remove an arbitrary element from a mutable HashSet in Scala
- Scala filtering on 2 conditions to remove strings from a list
More Query from same tag
- Why is foreach better than get for Scala Options?
- managed (ARM) in scala for nested resources
- Scala - what is the right way to organize singleton objects hierarchy?
- How and where to add an sbteclipse task?
- Cannot compile file in Scala
- Spark: Mapping elements of an RDD using other elements from the same RDD
- How to return all positives and the first negative number in a list using functional programming?
- Lagom microservice and Akka HTTP configuration location
- How to integrate Spark with Scala project in IntelliJ IDE?
- how to remove compactbuffer in spark output
- Output is not showing, spark scala
- Writing a test case for file uploads in Play 2.1 and Scala
- Spark Scala: Querying same table multiple times
- How to get error message from the scala/java MongoDB api
- Scala: List of class objects printing the class variable/attributes
- Play json: parse array of arrays of longs?
- Play JSON looses time info on deserialise
- Functional scala log accumulator
- Convert F# failwith to Scala
- Play Framework eating up disk space
- configure log4j for each spark job running on yarn mode
- Can we reverse Applicative of List?
- Create one hot encoded vector from category list in Spark
- reuse the result of a select expression in the "GROUP BY" clause?
- How to configure transactor in doobie?
- Why can I assign null to a Unit value and why does it get converted to ()?
- Parameterized simulation in Gatling load tests
- Scala: for-comprehension with guard inside reader
- Logstash scala log parsing
- Scala LinkedHashMap.toMap preserves order?