Python Forum
Newbie question for using map, lambda
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie question for using map, lambda
#1
Hello:
I have learned Python for a few days only, so have quite some questions. The following question(s) I want to ask is about the code translation. The original code I had was in F#, but I tried to convert it into Python, it works. But it seems a little ugly, so I want to see if anyone can advise how to improve it. The following is my code:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
from scipy import stats
import numpy as np

bidcv =
signs =
cvs =

a = [1.0, 2.0, 3.0, 4.0, 5.0]
b = [0.0, 1.0, 2.0, 3.0, 4.0]
c = [0.0, 0.1, 0.2, 0.3, 0.4]

d = np.array(list(zip(a, b, c)))
for d1 in d:
if (d1[2] == 0.0):
bidcv.append(d1[0] - d1[1])
else:
bidcv.append(d1[0])
print(bidcv)

for p1 in b:
if (p1 >= 0.0):
signs.append(1.0)
else:
signs.append(0.0)
print(signs)

cvArray = np.array(list(zip(bidcv, signs)))
for cv1 in cvArray:
cvs.append(cv1[0] * cv1[1])
print(cvs)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I have 3 arrays: a, b, c.
I created the 4th array d by zipping all 3 arrays: a, b, c
Using the 4th array d, I want to setup another array bidcv.
The rule for bidcv is: if the third array c's value is 0.0, then the corresponding bidcv value is: value of a minus value of b;
otherwise, the bidcv value is value of array a.
Using the second array b, I want to setup another array signs.
The rule for signs is: if the value of b is 0.0 and more, then the signs value is 1.0; if the value of b is negative, then the signs value is 0.0.
The final array is: cvs, which is made of product of array of signs by array of bidcv.
I want to have improvements in 3 areas:
For all the 3 for loops, I want them to be using either array.map or lambad function or combine of the both.
Please show me your code.
Thanks,
Reply


Messages In This Thread
Newbie question for using map, lambda - by zydjohn - Dec-09-2017, 12:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie question to return only the index of a dataframe zydjohn 0 2,626 Jan-22-2018, 03:40 PM
Last Post: zydjohn
  Newbie question how to find the coefficient for each variable zydjohn 10 12,453 Dec-14-2017, 05:01 PM
Last Post: j.crater
  Newbie question: how to generate dataframe and use multiple regression zydjohn 0 2,335 Dec-10-2017, 09:49 AM
Last Post: zydjohn
  Newbie question on how to use pandas.rolling_mean zydjohn 5 14,390 Dec-09-2017, 08:42 PM
Last Post: j.crater
  Filter and lambda question smw10c 3 7,668 Apr-27-2017, 04:44 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020