Thursday, December 26, 2024
Google search engine
HomeLanguagesCombining a one and a two-dimensional NumPy Array

Combining a one and a two-dimensional NumPy Array

Sometimes we need to combine 1-D and 2-D arrays and display their elements. Numpy has a function named as numpy.nditer(), which provides this facility.

Syntax: numpy.nditer(op, flags=None, op_flags=None, op_dtypes=None, order=’K’, casting=’safe’, op_axes=None, itershape=None, buffersize=0)

Example 1:

Python3




# importing Numpy package 
import numpy as np
  
num_1d = np.arange(5)
print("One dimensional array:")
print(num_1d)
  
num_2d = np.arange(10).reshape(2,5)
print("\nTwo dimensional array:")
print(num_2d)
  
# Combine 1-D and 2-D arrays and display 
# their elements using numpy.nditer() 
for a, b in np.nditer([num_1d, num_2d]):
    print("%d:%d" % (a, b),)


Output:

One dimensional array:
[0 1 2 3 4]

Two dimensional array:
[[0 1 2 3 4]
 [5 6 7 8 9]]
0:0
1:1
2:2
3:3
4:4
0:5
1:6
2:7
3:8
4:9

Example 2:

Python3




# importing Numpy package 
import numpy as np
  
num_1d = np.arange(7)
print("One dimensional array:")
print(num_1d)
  
num_2d = np.arange(21).reshape(3,7)
print("\nTwo dimensional array:")
print(num_2d)
  
# Combine 1-D and 2-D arrays and display 
# their elements using numpy.nditer() 
for a, b in np.nditer([num_1d, num_2d]):
    print("%d:%d" % (a, b),)


Output:

One dimensional array:
[0 1 2 3 4 5 6]

Two dimensional array:
[[ 0  1  2  3  4  5  6]
[ 7  8  9 10 11 12 13]
[14 15 16 17 18 19 20]]
0:0
1:1
2:2
3:3
4:4
5:5
6:6
0:7
1:8
2:9
3:10
4:11
5:12
6:13
0:14
1:15
2:16
3:17
4:18
5:19
6:20

Example 3:

Python3




# importing Numpy package 
import numpy as np
  
num_1d = np.arange(2)
print("One dimensional array:")
print(num_1d)
  
num_2d = np.arange(12).reshape(6,2)
print("\nTwo dimensional array:")
print(num_2d)
  
# Combine 1-D and 2-D arrays and display
# their elements using numpy.nditer() 
for a, b in np.nditer([num_1d, num_2d]):
    print("%d:%d" % (a, b),)


Output:

One dimensional array:
[0 1]

Two dimensional array:
[[ 0  1]
[ 2  3]
[ 4  5]
[ 6  7]
[ 8  9]
[10 11]]
0:0
1:1
0:2
1:3
0:4
1:5
0:6
1:7
0:8
1:9
0:10
1:11

RELATED ARTICLES

Most Popular

Recent Comments