![]() (lambda x, f: list((y for y in f(x))))(lst, lambda x: (sorted(y) for y in x)) If you generally want to have multiple statements within a lambda, you can pass other lambdas as arguments to that lambda. Putting the expressions in a list may simulate multiple expressions:Į.g.: lambda x: It's also usually considered clearer to use a list comprehension, which avoids the lambda altogether: for x in list_of_lists] So just use: map(lambda x: heapq.nsmallest(x,2), list_of_lists) It has O(N log(N)) complexity, while an O(n) solution exists. sorted(l) is not actually the best way for this. How should I get the second lowest item from each list in a sequence of lists?Ī. ![]() ![]() Note that this is probably what you should be using - it's bad practice for your map to have side effects on the original list. As alex's answer points out, sorted() is a version of sort that creates a new list, rather than sorting in-place, and can be chained. Can you get the second lowest item from a lambda by sorting the list?Ī. You can put the statements in a def instead. But you don't actually need to use a lambda. ![]() Can you put multiple statements in a lambda?Ī. There are several different answers I can give here, from your specific question to more general concerns. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |