1

I have the following function:

def func(x, y, z):
  d = -5.9
  T = 230.0

  f = x - y * T + z + d

  return f

From this, I want to make a single list of f-values extracted from func(x, y, z). The values x, y, and z are three different lists of values.

My question is, how do I make the list of f-values? I have tried the following, but it did not work properly:

f_list = [func(x, y, z) for x in x_list for y in y_list for z in z_list]
0

1 Answer 1

3

I think you want to use zip:

f_list = [func(*vals) for vals in zip(x_list, y_list, z_list)]

or for clearance:

f_list = [func(x, y, z) for x, y, z in zip(x_list, y_list, z_list)]

You can also use itertools.starmap:

import itertools
f_list  = list(itertools.starmap(func, zip(x_list, y_list, z_list)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.