简体   繁体   中英

Scala: Map list elements to a function of another list elements

I am very new to scala, I am trying to map the values of a list to the values obtained by running a function on the values of another list

What I mean is that I have an list A and another B. There is a function say func(b) which takes input as an element of the list B.

If I were to map it to list elements to a function of its own elements I could do it like this

val evalData = A.map(a=>(a, func(a)))

But I can't understand how to do it for my use case. Can someone please help me !! Thanks !

I suspect from your question that list A and B have the same length. For example, you could have:

val listA = List(1,2)
val listB = List(3,4)
def f(a:Int) = a+1
val result = listA zip (listB map f)
result: List[(Int, Int)] = List((1,4), (2,5))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM