'How to grab one value being returned from a function without rerunning the function? Python
Within my code, I am running two function which are both returning 3 things, (2 list, 1 float). I would like to add the two functions float values together to recieve the desired result.
Essentialy the code is
def fun1(...):
...
return list11, list12, float1
def fun2(...):
...
return list21, list22, float2
floatSum = float1+float2
I am able to make a graph from the returned list. However, I am not able to complete the floatSum calculation
Solution 1:[1]
If you only care about the float values you can do it like this:
_, _, float1 = fun1(...)
_, _, float2 = fun2(...)
float_sum = float1 + float2
This is an example of tuple unpacking:
https://www.pythontutorial.net/python-basics/python-unpacking-tuple/
Solution 2:[2]
Each function returns a tuple, you can access individual item in tuple by indexing like lists.
def fun1(...):
...
return list11, list12, float1
def fun2(...):
...
return list21, list22, float2
floatSum = fun1()[2]+fun2()[2]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Anentropic |
Solution 2 | huzzzus |