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: =========================================== 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:
=========================================== 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: ============================================ Add 60 to argument a, and return the result: code: =========================================== Both below have a similar function: Divide argument a by argument b, and return the result: code: ============= Divide argument a by argument b, and return the result: code: =========================================== Summarize by apply the function above to an argument by surrounding the function and its argument with parentheses: code: =========================================== Summarize argument a, b, c, and d , and return the result: code: =========================================== Multi-argument functions (functions that take more than one argument): code: ============================================ Modify a list: add items in between, and then merge all items. code: ============================================ Modify a list: add items in between, and then merge all items. code: =========================================== Calculation: code: =========================================== Call a functon: code: =========================================== Call rows in a csv file: code: =========================================== Call rows in a csv file: code: =========================================== 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: =========================================== Immediately invoked function expression: code: ================ Pass arguments to lambda expressions with immediately invoked function expression: code: =========================================== lambda function as a high-order function: code: ============================================ Both below have a similar function: Disassembler the lambda function for Python bytecode: code: ================== Disassembler a regular function object for Python bytecode: code: ============================================ Spreads across two lines because it is contained in a set of parentheses, but the lambda function remains a single expression: code: ============================================ Use decorators. Decorating the lambda function this way could be useful for debugging purposes: code: ============================================ outer_func() returns inner_func() as a closure: code: ================== lambda as a closure: code: ============================================ lambda as a closure: code: ================== lambda as a closure: code: ============================================ lambda to change letters to upper case: code: ============================================ Filter the words with a specific letter (case sensitive) : code: ============================================ Popup window with options: code: ============================================ Bind/link multiple commands to buttons: code: ============================================ Single options: code: ====================================================== Find the best word similarity with Word2Vec Models/word embeddings: code:
|
||||||||
| ================================================================================= | ||||||||
|
|
||||||||