Msquare
Msquare

Reputation: 373

Passing a nested list into a function and getting a nested list

** Based on Marcus.Aurelianus response, I have modified the question and executed the following code

def Bigfunction(G,T):
    return list(map(lambda x,y:10**-3*x*(1 + (y-25)),G,T)),list(map(lambda x,y:(x/1000)*(1 + (y-25)),G,T)), list(map(lambda x:(x/800),G))
G = list(range(100,1100,100))
T = list(range(25,40,10))
Iph_cal, Isc_cal, Tcel_cal = Bigfunction(G,T)
print(Iph_cal, Isc_cal, Tcel_cal)

**Output is:

[0.07000999999999999, 0.14464065999999998] [0.06999999999999999, 0.14461999999999997] [28.5, 32.0, 35.5, 39.0, 42.5, 46.0, 49.5, 53.0, 56.5, 60.0]

** In output: first and second list have given two elements only. Where as third list has given 10 elements, which is correct. Why the first and second list are not producing 10 elements.

Upvotes: 0

Views: 927

Answers (3)

bvidalar
bvidalar

Reputation: 96

I think it's better to define 3 separate functions for the calculations as you've done first. However, I would give them more descriptive names according to what they do. This way you'll be able to combine them as you want or even call them from other modules.

For instance:

def solar_current(G):
    return 0.03*G # Solar cell photo current in A

def shorcircuit_current(G,T):
    return (G/1000)*(T-25)  # short circuit current

def f3(G): # Whatever name it describes a bit better what it does.
    return G/800

T1, I1, I2 = [], [], []
for G in range(100, 1100, 100):
    T1.append(solar_current(G))
    I1.append(shortcircuit_current(G, 25))
    I2.append(f3(G))

PS: I assume the 25 you're passing as T value is an example. With the calculations done in shortcuit_current() it will always return 0

Upvotes: 0

Marcus.Aurelianus
Marcus.Aurelianus

Reputation: 1518

Use lambda and map function.

def Bigfunction(G,T):
    return list(map(lambda x:0.03*x,G)),list(map(lambda x,y:(x/1000)*(y-25),G,[T]*len(G))), list(map(lambda x:x/800,G))
G = list(range(100,1100,100))
T1, I1, I2 = Bigfunction(G,25)

Upvotes: 1

Teekeks
Teekeks

Reputation: 196

You try to return multiple values but you do not use the correct syntax. Try this:

def Bigfunction(G, T):
    return 0.03 * G, (G / 1000) * (T - 25), G / 800


T1 = []
T2 = []
T3 = []
for G in range(100, 1100, 100):
    a, b, c = Bigfunction(G, 25)
    T1.append(a)
    T2.append(b)
    T3.append(c)

Upvotes: 0

Related Questions