Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing items in two lists

Tags:

scala

I have two lists : List(1,1,1) , List(1,0,1)

I want to get the following :

  1. A count of every element that contains a 1 in first list and a 0 in the corresponding list at same position and vice versa. In above example this would be 1 , 0 since the first list contains a 1 at middle position and second list contains a 0 at same position (middle).

  2. A count of every element where 1 is in first list and 1 is also in second list. In above example this is two since there are two 1's in each corresponding list. I can get this using the intersect method of class List.

I am just looking an answer to point 1 above. I could use an iterative a approach to count the items but is there a more functional method ? Here is the entire code :

class Similarity {
  def getSimilarity(number1: List[Int], number2: List[Int]) = {
    val num: List[Int] = number1.intersect(number2)
    println("P is " + num.length)
  }
}

object HelloWorld {
  def main(args: Array[String]) {
    val s = new Similarity
    s.getSimilarity(List(1, 1, 1), List(1, 0, 1))
  }
}
like image 447
blue-sky Avatar asked Jul 23 '13 14:07

blue-sky


People also ask

How do you compare values in two lists?

counter() method can be used to compare lists efficiently. The counter() function counts the frequency of the items in a list and stores the data as a dictionary in the format <value>:<frequency>. If two lists have the exact same dictionary output, we can infer that the lists are the same.

How do you compare elements in a list?

Using sum() ,zip() and len() This method first compares each element of the two lists and store those as summation of 1, which is then compared with the length of the other list. For this method, we have to first check if the lengths of both the lists are equal before performing this computation.

How do I compare two lists in Excel for matches?

You can use the IF Function to compare two lists in Excel for matches in the same row. If Function will return the value TRUE if the values match and FALSE if they don't. You can even add custom text to display the word “Match” when a criterion is met and “Not a Match” when it's not met.

How do you compare two items in a list Python?

The cmp() function is a built-in method in Python used to compare the elements of two lists. The function is also used to compare two elements and return a value based on the arguments passed. This value can be 1, 0 or -1.


2 Answers

For the first one:

scala> val a = List(1,1,1)
a: List[Int] = List(1, 1, 1)

scala> val b = List(1,0,1)
b: List[Int] = List(1, 0, 1)

scala> a.zip(b).filter(x => x._1==1 && x._2==0).size
res7: Int = 1

For the second:

scala> a.zip(b).filter(x => x._1==1 && x._2==1).size
res7: Int = 2
like image 133
Jatin Avatar answered Sep 17 '22 13:09

Jatin


You can count all combinations easily and have it in a map with

def getSimilarity(number1 : List[Int] , number2 : List[Int]) = {

  //sorry for the 1-liner, explanation follows 
  val countMap = (number1 zip number2) groupBy (identity) mapValues {_.length}

}

/*
 * Example
 * number1 = List(1,1,0,1,0,0,1)
 * number2 = List(0,1,1,1,0,1,1)
 * 
 * countMap = Map((1,0) -> 1, (1,1) -> 3, (0,1) -> 2, (0,0) -> 1)
 */

The trick is a common one

// zip the elements pairwise

(number1 zip number2) 

/* List((1,0), (1,1), (0,1), (1,1), (0,0), (0,1), (1,1))
 *
 * then group together with the identity function, so pairs
 * with the same elements are grouped together and the key is the pair itself
 */ 

.groupBy(identity) 

/* Map( (1,0) -> List((1,0)), 
 *      (1,1) -> List((1,1), (1,1), (1,1)), 
 *      (0,1) -> List((0,1), (0,1)), 
 *      (0,0) -> List((0,0))
 * )
 *
 * finally you count the pairs mapping the values to the length of each list
 */

.mapValues(_.length)

/* Map( (1,0) -> 1, 
 *      (1,1) -> 3, 
 *      (0,1) -> 2, 
 *      (0,0) -> 1
 * )

Then all you need to do is lookup on the map

like image 20
pagoda_5b Avatar answered Sep 20 '22 13:09

pagoda_5b