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
#2
Please, use code tags when post code. Please see https://python-forum.io/Forum-Python-Cod...how-to-use

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]

# alternatives for bidcv
bidcv = [item[0] - item[1] if item[2] == 0.0 else item[0] for item in zip(a, b, c)]
bidcv1 = [item[0] - int(item[2] == 0.0) * item[1] for item in zip(a, b, c)]
bidcv2 = list(map(lambda x: x[0] if x[2] != 0.0 else x[0] - x[1], zip(a, b, c)))

print('bidcv:')
print(bidcv)
print(bidcv1)
print(bidcv2)


signs = [1.0 if item >= 0.0 else 0.0 for item in b]
print('signs:')
print(signs)

# alternatives for cv
cv = [item[0]*item[1] for item in zip(bidcv, signs)]
from operator import mul
cv1 = list(map(lambda x: mul(*x), zip(bidcv, signs)))
cv2 = [mul(*item) for item in zip(bidcv, signs)]
print('cv:')
print(cv)
print(cv1)
print(cv2)
Reply
#3
Great!
Your code works!
Thanks,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie question to return only the index of a dataframe zydjohn 0 2,522 Jan-22-2018, 03:40 PM
Last Post: zydjohn
  Newbie question how to find the coefficient for each variable zydjohn 10 12,000 Dec-14-2017, 05:01 PM
Last Post: j.crater
  Newbie question: how to generate dataframe and use multiple regression zydjohn 0 2,252 Dec-10-2017, 09:49 AM
Last Post: zydjohn
  Newbie question on how to use pandas.rolling_mean zydjohn 5 14,168 Dec-09-2017, 08:42 PM
Last Post: j.crater
  Filter and lambda question smw10c 3 7,537 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