multiple-variable-return

How to return more than one value from a function in Python? [duplicate]

故事扮演 提交于 2019-12-20 08:27:55
问题 This question already has answers here : What's the best way to return multiple values from a function in Python? (5 answers) Closed 6 years ago . How to return more than one variable from a function in Python? 回答1: You separate the values you want to return by commas: def get_name(): # you code return first_name, last_name The commas indicate it's a tuple, so you could wrap your values by parentheses: return (first_name, last_name) Then when you call the function you a) save all values to

Variable Return Type of a Method in C#

China☆狼群 提交于 2019-12-10 02:51:59
问题 I want to give a parameter to a method and i want my method to return data by looking the parameter. Data can be in type of boolean, string, int or etc. How can i return a variable type from a method? I don't want to return an object type and then cast it to another type. For example: BlaBla VariableReturnExampleMethod(int a) { if (a == 1) return "Demo"; else if (a == 2) return 2; else if (a == 3) return True; else return null; } The reason why i want that is i have a method that reads a

Returning every element from a list (Python)

大兔子大兔子 提交于 2019-12-07 04:53:14
问题 I know that it is possible for a function to return multiple values in Python. What I would like to do is return each element in a list as a separate return value. This could be an arbitrary number of elements, depending on user input. I am wondering if there is a pythonic way of doing so? For example, I have a function that will return a pair of items as an array, e.g., it will return [a, b] . However, depending on the input given, the function may produce multiple pairs, which will result

Returning every element from a list (Python)

我怕爱的太早我们不能终老 提交于 2019-12-05 10:08:15
I know that it is possible for a function to return multiple values in Python. What I would like to do is return each element in a list as a separate return value. This could be an arbitrary number of elements, depending on user input. I am wondering if there is a pythonic way of doing so? For example, I have a function that will return a pair of items as an array, e.g., it will return [a, b] . However, depending on the input given, the function may produce multiple pairs, which will result in the function returning [[a, b], [c, d], [e, f]] . Instead, I would like it to return [a, b], [c, d],

Variable Return Type of a Method in C#

烈酒焚心 提交于 2019-12-05 03:08:00
I want to give a parameter to a method and i want my method to return data by looking the parameter. Data can be in type of boolean, string, int or etc. How can i return a variable type from a method? I don't want to return an object type and then cast it to another type. For example: BlaBla VariableReturnExampleMethod(int a) { if (a == 1) return "Demo"; else if (a == 2) return 2; else if (a == 3) return True; else return null; } The reason why i want that is i have a method that reads a selected column of a row from the database. Types of columns are not same but i have to return every column

nargout in Python

余生长醉 提交于 2019-11-28 00:40:38
Does Python have any equivalent of nargout in MATLAB? I find nargout a very neat approach if we want to keep the number of return parameters flexible. Is there a way I can find out how many output parameters have been requested? Something like the following pseudo-python-code: def var_returns_func(a): """ a is a 1D numpy array """ if nargout==1: return a.sum() elif nargout==2: return a.sum(),a.std() So if I call this function as mean = var_returns_func(someNumPyarray) , it should return a single value. But if I call it as mean,std = var_returns_func(someNumPyarray) , it should return 2 values.

nargout in Python

北城余情 提交于 2019-11-26 21:46:14
问题 Does Python have any equivalent of nargout in MATLAB? I find nargout a very neat approach if we want to keep the number of return parameters flexible. Is there a way I can find out how many output parameters have been requested? Something like the following pseudo-python-code: def var_returns_func(a): """ a is a 1D numpy array """ if nargout==1: return a.sum() elif nargout==2: return a.sum(),a.std() So if I call this function as mean = var_returns_func(someNumPyarray) , it should return a

Linux bash: Multiple variable assignment

北慕城南 提交于 2019-11-26 06:57:05
问题 Does exist in linux bash something similar to the following code in PHP: list($var1, $var2, $var3) = function_that_returns_a_three_element_array() ; i.e. you assign in one sentence a corresponding value to 3 different variables. Let\'s say I have the bash function myBashFuntion that writes to stdout the string \"qwert asdfg zxcvb\". Is it possible to do something like: (var1 var2 var3) = ( `myBashFuntion param1 param2` ) The part at the left of the equal sign is not valid syntax of course. I\