简体   繁体   中英

Assigning variables to multiple 2-tuples returned from a function in Python

I have a function returning 3 2-tuples. What is the best way to call the function and assign Values1, Values2, Values3 to each tuple, so that I can print, say, (y1, y2) only? I couldn't find any answers that helped me further; thanks!

 def function(x, y, z, t):
     x1 = 
     x2 = 
     ...
     z2 = 
     return (x1, x2), (y1, y2), (z1, z2)

 Values1, Values2, Values3 = function(x, y, z, t) 

What is the best way to call the function and assign Values1, Values2, Values3 to each tuple, so that I can print, say, (y1, y2) only

If you are only interested in (y1, y2) and would like to ignore the other elements of the tuple, a general convention is to use _ (as a throw away variable name)

_, (y1, y2), _ = function(x, y, z, t) 

another option is to just store the value in a variable and then index it appropriately

value = function(x, y, z, t) 
(y1, y2) = value[1]

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