Menu Close

Mapping and Transposing with Map Module

Mapping values of different iterables

For example calculating the average of each i -th element of multiple iterables:

def Mod_average(*temp):
    return sum(temp)/len(temp)

Numb_1 = [1, 11, 111, 1111]
Numb_2 = [12, 17, 931, 122]
Numb_3 = [14, 10, 925, 141]
L = list(map(Mod_average, Numb_1, Numb_2, Numb_3))

print([round(i,2) for i in L])

# Output: [9.0, 12.67, 655.67, 458.0]

There are different requirements if more than one iterable is passed to map depending on the version of python:

Numb_1 = [1, 11, 111, 1111]
Numb_2 = [12, 17, 931, 122]
Numb_3 = [14, 10, 925, 141]

def median_of_three(a, b, x):
    return sorted((a, b, x))[1]
L = list(map(median_of_three, Numb_1, Numb_2))

print(L)
	

OUTPUT:
TypeError: median_of_three() missing 1 required positional argument: ‘x’

The mapping stops as soon as one iterable stops:

import operator

Numb_1 = [1, 11, 111, 1111]
Numb_2 = [12, 17, 931]

# Calculate difference between elements
L = list(map(operator.sub, Numb_1, Numb_2))
print(L) #OUT: [-11, -6, -820]

L = list(map(operator.sub, Numb_2, Numb_1))
print(L)#OUT:  [11, 6, 820]

'''
OUTPUT:  
	 [-11, -6, -820]
	 [11, 6, 820]
'''

Morae Q!

  1. OS operating system module using path parameter.
  2. Find the smallest and largest integers after sorting using bubble sort.
  3. Find the integer and its number of occurrences.
  4. Algorithm complexity – Big O Notation with Examples.
  5. Linear search.
  6. Map module using series mapping and parallel mapping.
  7. Mapping and Transposing with Map Module.
  8. Map Module/built-in Function.
  9. Linux commands for managing files.
  10. Program which takes two lists and maps two lists into a dictionary.
  11. Splitting strings and substituting using regular expressions regex.
  12. Basics of regular expression Regex.
  13. Find the square value of the dictionary.
  14. Check whether the given key is present in the dictionary.
  15. Matching an expression only in specific places using regular expressions.
  16. Escaping special characters using regular expressions.
  17. Check the key.
  18. Grouping and sub-grouping using regular expressions.
  19. Generate a Dictionary that Contains Numbers in the Form (x , x*x).
  20. Algorithm complexity Big-Theta, Big-Omega and Big-O Notations.