Open In App

Combining a one and a two-dimensional NumPy Array

Last Updated : 01 Oct, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Next Article
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy