Trying to understand lambda

When i do

dict = {'Alice': '7898', 'Beth': '9102', 'Cecil': '3258'} print filter(lambda x: x, dict['Alice']) 

he shows: 7898

When will I do the next

 dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'} print filter(lambda x: x=="2341", dict['Alice']) 

He shows:

Why it does not display True. How to become true?

+4
source share
6 answers

It looks like your misunderstanding with filter is for me. You pass the predicate function and the iterable object to filter . It creates a new object containing these elements from the first iterable, for which the predicate returns the true value (not necessarily True itself, just something that checks as true). Iterable in two cases is the string '2341' , which means that individual letters are checked. Of course, the string '2341' not equal to any of the '2' , ' 3' , '4' or '1' .

Try this with a tuple and it will be easier for you to see what happens:

 >>> tup = tuple(dict['Alice']) >>> tup ('7', '8', '9', '8') >>> filter(lambda x: x, tup) ('7', '8', '9', '8') >>> tup ('7', '8', '9', '8') >>> filter(lambda x: x, tup) ('7', '8', '9', '8') >>> filter(lambda x: x=="2341", tup) () 
+4
source

filter () performs the following actions: sets the function and iterability (for example, list, tuple, etc.), passes each element to the list of functions. For each element, the function returns a boolean value of true or false. If the function returns true for the item, the item is added to the new list.

When the filter is finished, it returns a new list with all the selected items. This allows you to β€œfilter” the list based on criteria and select only items that match the criteria.

This is where the difficult thing happens. filter () goes through any iterable. This includes the string. When you pass dict ['Alice'] as the object to iterate over, it passes "2341" and triggers a filter for each character in the string. You can break the filter logic as follows:

 def matches(x): return x == '2341' result = '' for char in x: if matches(char): result += char print result 

This does not work, because none of your individual characters is equal to "2341".

+5
source

You want to check if the entry for "Alice" is "2341". You can do it through

 print dict['Alice'] == '2341' 

The problem you are facing is not the lambda form, but the filter method, which is not suitable in this case.

In the general case, the lambda form is more or less than an anonymous function (see, for example, here ).

+3
source

It is possible that you are trying to do:

 >>> dic = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'} >>> [i for i in dic if dic[i] == '2341'] ['Alice'] 

And if you add other elements with the same value, you will get all of them in the list

+1
source

If you just want to call the lambda function, you just use it as a function:

 print (lambda x: x=="2341")(dict["Alice"]) 

This gives the expected result (true).

When you use filter , it treats its second argument as a list, so "2341" is treated as a list of characters ['2', '3', '4', '1'] , none of which are equal to β€œ2341”.

An interesting thing that I just found out: Python 3 returns filter objects from the filter() function, so the proper use of a filter becomes

 print( list( filter( lambda x: x, dict['Alice'] ) ) ) 

And this returns ['2', '3', '4', '1'] , which would have avoided the initial confusion.

0
source

Filter is usually applied to two arguments:

  • function
  • list (or other iterable object)

filter applies a function to each object in the list and returns a list of all objects for which the returned function is True .

In your second example, your first argument is a function, as expected. But your second argument is not a list - it is the string "2341" (the search result for "Alice" in the dictionary).

( Change ). At first I got this next part wrong. Thanks to other posters for making the right choice.) The filter treats the string as a list, applying a function to each character and saving only those characters for which it returned True (none of them), which led to an empty string. Now, if you look back at your first example, it has the same problem, and only (bad?) The luck in the answer was what you expected.

As another poster, perhaps you want to apply your function more directly. In the first example:

 (lambda x: x)(dict['Alice']) 

In the second example:

 (lambda x: x=="2341")(dict['Alice']) 
0
source

All Articles