How to use Map In Python

The map is a function used to execute a function for each item in an Iterable i.e array.

Example:

Here, we first are importing Numpy and defining the 1d Array of Tuples. This Array contains a 0D Array i.e the tuples further using the Map function we are going through each item in the array, and converting them to an NDArray.The map object is being converted to a list array and then to an NDArray and the array is printed further at the last, we are checking that dimension of the resulting Array using the ndim property.

Python3




# importing Numpy
import numpy as np
  
# 1d Array of Tuple
arr = [(1, 2, 3), ('Hi', 'Hello', 'Hey')]
x = map(np.array, arr)
  
# Changing map object to a list, then 
# to an NDarray
x = np.array(list(x))
print(x)
  
# Checking the Dimension of the Resulting
# NDArray
print(x.ndim)


Output:

[['1' '2' '3']
['Hi' 'Hello' 'Hey']]
2

How to convert 1D array of tuples to 2D Numpy array?

In this article, we will discuss how to convert a 1D array of tuples into a numpy array.

Example:

Input: [(1,2,3),(‘Hi’,’Hello’,’Hey’)]

Output: [[‘1’ ‘2’ ‘3’] [‘Hi’ ‘Hello’ ‘Hey’]] #NDArray

Similar Reads

Method 1: Using Map

The map is a function used to execute a function for each item in an Iterable i.e array....

Method 2: The Naive Method

...

Contact Us