Python Automation and Machine Learning for EM and ICs

An Online Book, Second Edition by Dr. Yougui Liao (2024)

Python Automation and Machine Learning for EM and ICs - An Online Book

Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

map() function

When managing sizable iterables and aiming for enhanced resource control, opting for map() instead of a loop becomes essential. In Python, the map() function holds significance for handling iterables such as tuples and lists. Essentially, it facilitates the processing and transformation of iterable items without the need for explicit looping, thus streamlining operations.

We can consider the map() function as a tool for applying a specified function to every item in an iterable and presenting the results as a list. This function requires two parameters: the function to be applied and the iterable on which it operates.

The syntax looks like this:

 

map(function, iterable)

Alternatively,

 

map(function, iterable[iterable1, iterable2, iterable3, … ])

The map() function offers greater efficiency compared to a loop. Additionally, it provides items as needed, while a loop requires storing values in memory. Consider a scenario with a vast number of iterables; utilizing a loop would entail storing a significant amount of data in memory, whereas with the map() function, this concern is alleviated. With map(), you achieve equivalent functionality without incurring the substantial overhead associated with loops. This aspect becomes increasingly critical as your Python applications grow in size and complexity.

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

Application of map(function, iterable). Code:

 

Output:

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

Uses the map() function along with a lambda function to add corresponding elements from numbers1 and numbers2. Code:

 

Output:

The script above uses the map() function along with a lambda function to add corresponding elements from numbers1 and numbers2. For example, if numbers1 = [3, 6, 9, 12, 15] and numbers2 = [21, 24, 27, 30, 33], the script will compute [3+21, 6+24, 9+27, 12+30, 15+33], resulting in the list [24, 30, 36, 42, 48].

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

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

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

lambda function.  Code:
         Upload Files to Webpages
       Output:    
         Upload Files to Webpages