Filtering list items in Python?

I want to filter items from a list of lists and iterate over the items of each item using lambda. For example, given a list:

a = [[1,2,3],[4,5,6]] 

Suppose I want to contain only elements where the sum of the list is greater than N. I tried to write:

 filter(lambda x, y, z: x + y + z >= N, a) 

but I get the error:

  <lambda>() takes exactly 3 arguments (1 given) 

How can I iterate when assigning values ​​to each element x, y and z? Something like zip, but for arbitrarily large lists.

thanks,

ps I know that I can write this with: filter (lambda x: sum (x) ..., a), but this is not so, imagine that these are not numbers, but arbitrary elements, and I wanted to assign values ​​to their names variables.

+4
source share
7 answers

Using lambda with filter is kind of dumb when we have other methods available.

In this case, I will probably solve the specific problem this way (or using an equivalent generator expression)

 >>> a = [[1, 2, 3], [4, 5, 6]] >>> [item for item in a if sum(item) > 10] [[4, 5, 6]] 

or if I need to unpack, for example

 >>> [(x, y, z) for x, y, z in a if (x + y) ** z > 30] [(4, 5, 6)] 

If I really needed a function, I could use the unpacking of the tuple arguments (which, by the way, was removed in Python 3.x, since people don’t use it): lambda (x, y, z): x + y + z takes a tuple and unpacks the three elements x , y and z . (Note that you can also use this in def , i.e.: def f((x, y, z)): return x + y + z .)

You can, of course, use unpacking the destination style ( def f(item): x, y, z = item; return x + y + z ) and indexing ( lambda item: item[0] + item[1] + item[2] ) in all versions of Python.

+24
source

You can explicitly name sublist items (provided that there is always a fixed number of them) by providing a lambda tuple as an argument:

 >>> a = [[1,2,3],[4,5,6]] >>> N = 10 >>> filter(lambda (i, j, k): i + j + k > N, a) [[4, 5, 6]] 

If you specify "lambda i, j, k" as you tried to do this, you say that lambda will receive three arguments. But the filter will give lambda to each element a, i.e. one sublist at a time (thus, you got an error). By including arguments in lambda in parentheses, you say that lambda will receive one argument, but you also name each of its components.

+7
source

You can do something like

 >>> a=[[1,2,3],[4,5,6]] >>> filter(lambda (x,y,z),: x+y+z>6, a) [[4, 5, 6]] 

Using deconstruction syntax.

+1
source

Try something like this:

 filter(lambda a: a[0] + a[1] + a[2] >= N, a) 
0
source

Use the function instead of lambda, then myVar = a[0] , etc.

0
source

How about this?

filter(lambda b : reduce(lambda x, y : x + y, b) >= N, a)

This does not answer the question asked, I know, but it works for arbitrarily long lists and arbitrarily long subscriptions and supports any operation that works under reduce() .

0
source

Well, you know you can use sum . The goal of what you are trying to do seems a bit vague, but I think the optional parameter syntax may help you, or at least give you some inspiration. If you put the * parameter before the parameter, it creates a tuple of itself and all other parameters. If you put ** in front of it, you will get a dictionary.

To see this:

 def print_test(a,b,c,*d): print a print b print c print d print_test(1,2,3,4,5,6) 

prints

 1 2 3 (4, 5, 6) 

You can also use this syntax with lambda .

As I said, I'm not quite sure what you are trying to do, but it looks like this might help. I don't think you can get local variable assignments in lambda without any hacking, but maybe you can use this to somehow assign values ​​to the variables.

Edit: Ah, I understand that you are looking for moreso now. I think you want:

 lambda (a, b, c): a+b+c > N 
0
source

Source: https://habr.com/ru/post/1307186/


All Articles