-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9f. Q3.py
More file actions
19 lines (15 loc) · 719 Bytes
/
9f. Q3.py
File metadata and controls
19 lines (15 loc) · 719 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
'''Write a program that defines a function create_array() to create
and return a 3D array whose dimensions are passed to the function.
Also initialize each element of this aray to a value passed to the function.
e.g. create_array(3,4,5,n) where first three arguments are 3D array dimensions
and 4th value is for initialing each value of the 3D array.'''
def create_array(x, y, z, value):
# Create a 3D array using nested list comprehensions
return [[[value for _ in range(z)] for _ in range(y)] for _ in range(x)]
# Example usage
array = create_array(3, 4, 5, 7)
# Print the 3D array
for i, layer in enumerate(array):
print(f"Layer {i}:")
for row in layer:
print(row)