0
constraint_condition = [lambda x: [x[0], x[1]]]
mid.gen_answer(constraint_ueq)

In codes above, mid is a class, and the method gen_answer is calling a function from another python package which requires n-dimension input written in this format: lambda x:[x[0],x[1],...x[n]]

How do I create lambda function and fill these paramters automatically? For example I want a 4-dimension input, I shall be writing this by my hand like this: lambda x:[x[0],x[1],x[2],x[3]], but what if there are more dimensions(like 12)? There is a way to let python write these (x[1],x[2]...) for me?

3
  • is len(x) == n? Commented Nov 4, 2022 at 14:54
  • lambda x: [sub_x for sub_x in x]? Commented Nov 4, 2022 at 14:56
  • to add to @jfaccioni one liner, if you want less indexes than whole length of x, you can do in x[:k] where k is the number of dims. Commented Nov 4, 2022 at 14:58

1 Answer 1

1

Guy, I solved this by using lambda x:[sub_x for sub_x in x[:n]]. Thx for your help

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.