Electron microscopy
 
lambda function in Python
- Python Automation and Machine Learning for ICs -
- An Online Book -
Python Automation and Machine Learning for ICs                                   http://www.globalsino.com/ICs/        


=================================================================================

 

A lambda function is a small anonymous function, which can take any number of arguments, but can only have one expression. Syntax of the lambda function:
         i) It can only contain expressions and can’t include statements in its body.
         ii) It is written as a single line of execution.
         iii) It does not support type annotations.
         iv) It can be immediately invoked.

===========================================

In this example, we have a list of numbers. We use the filter() function with a lambda function to filter out numbers greater than 5. The lambda function checks each number in the list and returns True if the number is greater than 5, effectively filtering out numbers less than or equal to 5. Finally, we convert the filtered result into a list and print it. Code:

         

       Output:    

         

===========================================

In this script, we have a list of numbers. We use the filter() function with a lambda function to filter out only the numbers that are divisible by both 2 and 3. The lambda function checks if the number modulo 2 equals 0 (i.e., it's divisible by 2) and if the number modulo 3 equals 0 (i.e., it's divisible by 3), and returns True if both conditions are met. Finally, we convert the filtered result into a list and print it.. Code:

         

       Output:    

         

===========================================

In this script below, we have a list of names. We use the filter() function with a lambda function to filter out only the names that contain the letter 'a'. The lambda function checks if the lowercase version of each name contains the letter 'a' using the in keyword. Finally, we convert the filtered result into a list and print it. Code:

         

       Output:    

         

===========================================

A script filters out words from the given list that contain the letter 'I' and prints the resulting list: 1) lambda x: 'I' in x: This is a lambda function that takes a string x as input and returns True if the letter 'I' is present in x, otherwise False, 2) filter(lambda x: 'I' in x, ['Italy', 'either', 'sun']): The filter() function takes two arguments: a function and an iterable. It applies the function to each element of the iterable and returns only those elements for which the function returns True. In this case, it filters out the words from the list ['Italy', 'either', 'sun'] that contain the letter 'I', 3) list(...): Converts the filtered result into a list, and 4) print(...): Prints the resulting list. Code:

   Output:

============================================

Check if two lists are identical. The map() function is used to apply the lambda function lambda m, k: m == k to pair-wise elements of ListA and ListB, returning a list of Boolean values indicating whether each pair of elements is equal. The reduce() function from the functools module is then used to reduce the list of Boolean values to a single Boolean value indicating if all pairs are equal.  Code:
         Upload Files to Webpages
       Output:    
         Upload Files to Webpages

============================================

Add 60 to argument a, and return the result: code:
        Add 60 to argument a, and return the result
Output:
        67

===========================================

Both below have a similar function:

Divide argument a by argument b, and return the result: code:
        Add 60 to argument a, and return the result
Output:
        2.0

=============

Divide argument a by argument b, and return the result: code:
        Add 60 to argument a, and return the result
Output:
        2.0

===========================================

Summarize by apply the function above to an argument by surrounding the function and its argument with parentheses: code:
        Add 60 to argument a, and return the result
Output:
        3

===========================================

Summarize argument a, b, c, and d , and return the result: code:
        Add 60 to argument a, and return the result
Output:
        10

===========================================

Multi-argument functions (functions that take more than one argument): code:
        Add 60 to argument a, and return the result
Output:
        Here is my introduction: My full name is Yougui Liao

============================================

Modify a list: add items in between, and then merge all items. code:
         Output the rows if the cell value is in a specific range
Output:         
         Output the rows if the cell value is in a specific range

============================================

Modify a list: add items in between, and then merge all items. code:
         Output the rows if the cell value is in a specific range
Output:         
         Output the rows if the cell value is in a specific range

===========================================

Calculation: code:
        Add 60 to argument a, and return the result

===========================================

Call a functon: code:
        Add 60 to argument a, and return the result

===========================================

Call rows in a csv file: code:
        Add 60 to argument a, and return the result

===========================================

Call rows in a csv file: code:
        Add 60 to argument a, and return the result

===========================================

The ‘_’ variable name is usually a name for an ignored variable. This function below can be used when we want to get a specific output for every input: code:
        Add 60 to argument a, and return the result

===========================================

Immediately invoked function expression: code:
        Add 60 to argument a, and return the result
Output:
        5

================

Pass arguments to lambda expressions with immediately invoked function expression: code:
        Add 60 to argument a, and return the result
Output:
        10
        10

===========================================

lambda function as a high-order function: code:
        Add 60 to argument a, and return the result
Output:
        30
        13

============================================

Both below have a similar function:

Disassembler the lambda function for Python bytecode: code:
        Add 60 to argument a, and return the result
Output:
        Add 60 to argument a, and return the result

==================

Disassembler a regular function object for Python bytecode: code:
        Add 60 to argument a, and return the result
Output:
        Add 60 to argument a, and return the result

============================================

Spreads across two lines because it is contained in a set of parentheses, but the lambda function remains a single expression: code:
        Add 60 to argument a, and return the result
Output:
        odd
        even        

============================================

Use decorators. Decorating the lambda function this way could be useful for debugging purposes: code:
         Sum the numbers of a defined list
Output:         
         Sum the numbers of a defined list

============================================

outer_func() returns inner_func() as a closure: code:
         Sum the numbers of a defined list
Output:         
         Sum the numbers of a defined list

==================

lambda as a closure: code:
         Sum the numbers of a defined list
Output:         
         Sum the numbers of a defined list

============================================

lambda as a closure: code:
         Sum the numbers of a defined list
Output:         
         7         
         7         
         7         
         7

==================

lambda as a closure: code:
         Sum the numbers of a defined list
Output:         
         1         
         9         
         6         
         7

============================================

lambda to change letters to upper case: code:
         Sum the numbers of a defined list
Output:         
         ['USA', 'CANADA', 'ITALY']

============================================

Filter the words with a specific letter (case sensitive) : code:
         Sum the numbers of a defined list
Output:         
         ['Italy']

============================================

Popup window with options: code:
         Pop-up windows
         Pop-up windows
Output:         
         Pop-up windows
         Pop-up windows
         Pop-up windows
         Pop-up windows
         Pop-up windows
         Pop-up windows

============================================

Bind/link multiple commands to buttons: code:          
         Bind/link multiple commands to buttons
Output: popup window:          
          Bind/link multiple commands to buttons
Output after clicking "Button A":          
          Bind/link multiple commands to buttons
Output after clicking "Button B":          
          Bind/link multiple commands to buttons

============================================

Single options: code:          
          Launch the existing opened application if there is or start a new one if there is not
Output 1:          
         Launch the existing opened application if there is or start a new one if there is not         
Output 2:          
         Launch the existing opened application if there is or start a new one if there is not
         Launch the existing opened application if there is or start a new one if there is not         
Output 3:          
         Launch the existing opened application if there is or start a new one if there is not
         Launch the existing opened application if there is or start a new one if there is not         
Output 4:          
         Launch the existing opened application if there is or start a new one if there is not
         Launch the existing opened application if there is or start a new one if there is not

======================================================

Find the best word similarity with Word2Vec Models/word embeddings: code:          
          Find the best similarity with Word2Vec Models/word embeddings
          Find the best similarity with Word2Vec Models/word embeddings
          Find the best similarity with Word2Vec Models/word embeddings
Input (csv file):                  
          Find the best similarity with Word2Vec Models/word embeddings
Output:                  
          Find the best similarity with Word2Vec Models/word embeddings
          Find the best similarity with Word2Vec Models/word embeddings


 



 

=================================================================================