+ 3

Please how do we make lambda take 2 or more arguments?

Lambda

4th Jan 2020, 5:28 PM
Yusuf Shuaib Olalekan
Yusuf Shuaib Olalekan - avatar
36 Answers
+ 13
By separating them by commas. lambda x, y, z: None Takes 3 arguments.
4th Jan 2020, 5:29 PM
Seb TheS
Seb TheS - avatar
+ 7
(lambda x, y, z: x * y + z)(6, 4, 11) #35
4th Jan 2020, 5:34 PM
Seb TheS
Seb TheS - avatar
+ 5
Yusuf Shuaib Olalekan Your task is not clear. You wrote: ---- "What I want to achieve is to filter a list of numbers and find any three number in that list whose sum is not more than another know (sic) number" ---- Are you saying the sum of all 3 numbers or any 2 out of the 3 numbers cannot exceed any known numbers? Are known numbers those numbers not included in the filtered list? Assuming known numbers don't include any of the 3 in the list and the sum is based on all 3 filtered numbers, then wouldn't the answer be the 3 lowest values only if their sum is lower than the 4th lowest value? Otherwise, the result would be None? The details matter. Otherwise, it's unclear what you are attempting to accomplish.
4th Jan 2020, 7:44 PM
David Carroll
David Carroll - avatar
+ 5
Yusuf Shuaib Olalekan Actually I think I have a solution now, you need to sort the list, from min to max. Then: compare sum(l[i:i + 3]) to l[i + 3] if condition: slice l[i:i + 3] off else: i += 1 repeat until i + 3 == len(l) or len(l) < 4
4th Jan 2020, 8:01 PM
Seb TheS
Seb TheS - avatar
+ 5
Yusuf Shuaib Olalekan Ah... yes... that makes complete sense. šŸ˜‰šŸ‘Œ So in the case of using the list [1,3,5,6,8] for the number 19, multiple valid answers could be any combination except: [5,6,8] But for the number 9, the only valid combination would be: [1,3,5] And for 12, all possible combinations are: [1,3,5] [1,3,6] [1,3,8] Thanks for that clarification. The examples are always great for making these much clearer.
4th Jan 2020, 8:25 PM
David Carroll
David Carroll - avatar
+ 5
Jack, jump into the tutorial of the language you want to learn, and start learning. Don't forget to practice with your own hands everything you read about. And please, don't ask your questions in the question of another person. First use the search function, and if that doesn't give you an answer, post a new question of your own.
6th Jan 2020, 12:33 PM
HonFu
HonFu - avatar
+ 4
Yusuf Shuaib Olalekan I think that the best tool is nested for loop.
4th Jan 2020, 6:03 PM
Seb TheS
Seb TheS - avatar
+ 3
Yusuf Shuaib Olalekan You can use any statements that have ability to evaluate to something. You can use comparison with any amount of arguments, but you can't use if statement, but you can use ternary operator.
4th Jan 2020, 5:39 PM
Seb TheS
Seb TheS - avatar
+ 3
Do you want to get all sublist that meet the condition, or just one and you don't care which?
6th Jan 2020, 10:50 AM
HonFu
HonFu - avatar
+ 2
Seb TheS Alright, I will give a trial and tell you the outcome.
4th Jan 2020, 6:13 PM
Yusuf Shuaib Olalekan
Yusuf Shuaib Olalekan - avatar
4th Jan 2020, 5:33 PM
Seb TheS
Seb TheS - avatar
+ 1
Yusuf Shuaib Olalekan I don't think filter would be the right tool for that.
4th Jan 2020, 5:42 PM
Seb TheS
Seb TheS - avatar
+ 1
TypeError?
4th Jan 2020, 6:41 PM
Seb TheS
Seb TheS - avatar
+ 1
Show
4th Jan 2020, 6:42 PM
Seb TheS
Seb TheS - avatar
+ 1
Yusuf Shuaib Olalekan How TypeError was described?
4th Jan 2020, 6:48 PM
Seb TheS
Seb TheS - avatar
+ 1
3d loops are required, because function takes 3 arguments.
4th Jan 2020, 6:53 PM
Seb TheS
Seb TheS - avatar
+ 1
David Carroll I have a list containing numbers (say [1,3,5,6,8]), and another number (say, 12). Then I want to get a list containing any three number from my first list, such that the sum of the content of the new list will not <= the other number(12). Do you understand?
4th Jan 2020, 7:55 PM
Yusuf Shuaib Olalekan
Yusuf Shuaib Olalekan - avatar
+ 1
Yusuf Shuaib Olalekan I got the idea work, but I've no idea what you did to get None.
5th Jan 2020, 1:13 PM
Seb TheS
Seb TheS - avatar
+ 1
E. G: a = lambda *s:sum(s) print(a(1,2,3,4,5)) This will print out 15
6th Jan 2020, 10:39 AM
Vuyisile Lucas Ncipha
Vuyisile Lucas Ncipha - avatar