简体   繁体   中英

What is the equivalent of Scala's map case in Python

In Scala, you could write expressions like

A.map{case (x, y) => (y, y)}

I was wondering, how we write the equivalent statement in Python?

I don't know Scala, so I have to guess a bit. This looks like you are mapping a function taking two arguments over a list of pairs, and replace each pair with two times the second element. In Python, you would use a list comprehension for this purpose:

[(y, y) for x, y in A]

The Scala function probably uses pattern matching to assign the value of x and y . Python doesn't have pattern matching, but in this simple case sequence unpacking achieves the same goal.

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