简体   繁体   English

将变量分配给从Python中的函数返回的多个2元组

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

I have a function returning 3 2-tuples. 我有一个返回3个2元组的函数。 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? 调用该函数并为每个元组分配Values1,Values2,Values3的最佳方法是什么,以便我只能打印说(y1,y2)? 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 调用函数并将Values1,Values2,Values3分配给每个元组的最佳方法是什么,以便我只能打印说(y1,y2)

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)感兴趣,并且想忽略元组的其他元素,则通常的约定是使用_(作为丢弃变量名)

_, (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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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