Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – 3D Matrix to Coordinate List
When working with 3D matrices in Python, you might need to convert them into coordinate pairs. This process uses list comprehension and the zip() function to pair corresponding elements from different sublists.
Understanding 3D Matrix Structure
A 3D matrix in Python is essentially a list containing multiple 2D matrices (lists of lists). Each 2D matrix contains rows of data that can be paired together ?
# 3D matrix structure: [2D_matrix1, 2D_matrix2, 2D_matrix3]
# Each 2D matrix: [[row1], [row2]]
matrix_3d = [
[['He', 'Wi'], ['llo', 'll']], # First 2D matrix
[['Pyt', 'i'], ['hon', 'sFun']], # Second 2D matrix
[['Ho', 'g'], ['pe', 'ood']] # Third 2D matrix
]
print("3D Matrix structure:")
for i, matrix_2d in enumerate(matrix_3d):
print(f"Matrix {i+1}: {matrix_2d}")
3D Matrix structure: Matrix 1: [['He', 'Wi'], ['llo', 'll']] Matrix 2: [['Pyt', 'i'], ['hon', 'sFun']] Matrix 3: [['Ho', 'g'], ['pe', 'ood']]
Converting to Coordinate List
The conversion process pairs corresponding elements from the two rows of each 2D matrix using zip() ?
matrix_3d = [
[['He', 'Wi'], ['llo', 'll']],
[['Pyt', 'i'], ['hon', 'sFun']],
[['Ho', 'g'], ['pe', 'ood']]
]
print("Original 3D matrix:")
print(matrix_3d)
# Sort the matrix (optional step)
matrix_3d.sort()
print("\nAfter sorting:")
print(matrix_3d)
# Convert to coordinate list using list comprehension and zip
coordinate_list = [coordinate for row1, row2 in matrix_3d for coordinate in zip(row1, row2)]
print("\nCoordinate list:")
print(coordinate_list)
Original 3D matrix:
[[['He', 'Wi'], ['llo', 'll']], [['Pyt', 'i'], ['hon', 'sFun']], [['Ho', 'g'], ['pe', 'ood']]]
After sorting:
[[['He', 'Wi'], ['llo', 'll']], [['Ho', 'g'], ['pe', 'ood']], [['Pyt', 'i'], ['hon', 'sFun']]]
Coordinate list:
[('He', 'llo'), ('Wi', 'll'), ('Ho', 'pe'), ('g', 'ood'), ('Pyt', 'hon'), ('i', 'sFun')]
How It Works
The list comprehension breaks down into these steps ?
# Step-by-step breakdown
matrix_3d = [[['He', 'Wi'], ['llo', 'll']], [['Ho', 'g'], ['pe', 'ood']]]
print("Step-by-step process:")
for i, (row1, row2) in enumerate(matrix_3d):
print(f"\n2D Matrix {i+1}:")
print(f" Row 1: {row1}")
print(f" Row 2: {row2}")
zipped = list(zip(row1, row2))
print(f" Zipped: {zipped}")
Step-by-step process:
2D Matrix 1:
Row 1: ['He', 'Wi']
Row 2: ['llo', 'll']
Zipped: [('He', 'llo'), ('Wi', 'll')]
2D Matrix 2:
Row 1: ['Ho', 'g']
Row 2: ['pe', 'ood']
Zipped: [('Ho', 'pe'), ('g', 'ood')]
Practical Example with Numbers
Here's a clearer example using numerical coordinates ?
# 3D matrix with x and y coordinates
coordinates_3d = [
[[1, 2, 3], [4, 5, 6]], # x-coords and y-coords for set 1
[[7, 8, 9], [10, 11, 12]], # x-coords and y-coords for set 2
[[13, 14], [15, 16]] # x-coords and y-coords for set 3
]
print("3D coordinate matrix:")
print(coordinates_3d)
# Convert to (x,y) coordinate pairs
coordinate_pairs = [pair for x_coords, y_coords in coordinates_3d
for pair in zip(x_coords, y_coords)]
print("\nCoordinate pairs (x, y):")
for i, pair in enumerate(coordinate_pairs):
print(f"Point {i+1}: {pair}")
3D coordinate matrix: [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14], [15, 16]]] Coordinate pairs (x, y): Point 1: (1, 4) Point 2: (2, 5) Point 3: (3, 6) Point 4: (7, 10) Point 5: (8, 11) Point 6: (9, 12) Point 7: (13, 15) Point 8: (14, 16)
Conclusion
Converting a 3D matrix to coordinate list uses list comprehension with zip() to pair corresponding elements from two sublists. This technique is useful for transforming nested data structures into coordinate pairs for plotting or mathematical operations.
