Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to define and use 3 dimensional numpy array in Python

3 Answers

0 votes
import numpy as np
 
arr = np.array([[[2, 4, 6],[8, 10, 12]],
                [[14, 16, 18],[20, 22, 24]]])
               
print(arr)
print
print(arr[...,1])
print
print(arr[: ,: ,1])
print
print(arr[: ,: ,2])
 
 
'''
run:
 
[[[ 2  4  6]
  [ 8 10 12]] 
 [[14 16 18] 
  [20 22 24]]]
  
[[ 4 10]
 [16 22]]

[[ 4 10]
 [16 22]]

[[ 6 12]
 [18 24]]
 
'''

 



answered Jan 25, 2019 by avibootz
edited Jan 27, 2019 by avibootz
0 votes
import numpy as np

arr = np.zeros((5,4,3))

arr[0,0,0] = 2
arr[0,0,1] = 4
arr[1,0,2] = 6
              
print(arr)



'''
run:

[[[2. 4. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]
             
 [[0. 0. 6.] 
  [0. 0. 0.] 
  [0. 0. 0.] 
  [0. 0. 0.]]
             
 [[0. 0. 0.] 
  [0. 0. 0.] 
  [0. 0. 0.] 
  [0. 0. 0.]]
             
 [[0. 0. 0.] 
  [0. 0. 0.] 
  [0. 0. 0.] 
  [0. 0. 0.]]
             
 [[0. 0. 0.] 
  [0. 0. 0.] 
  [0. 0. 0.] 
  [0. 0. 0.]]]

'''

 



answered Jan 25, 2019 by avibootz
edited Jan 27, 2019 by avibootz
0 votes
import numpy as np
  
arr = np.array([[[2, 4, 6, 56],[8, 10, 12, 89]],
                [[14, 16, 18, 43],[20, 22, 24, 76]]])
                
print(arr)
print
print(arr[1:2 ,1:3])
print
print(arr[0:1])
print
print(arr[1:2])
 
  
  
'''
run:
  
[[[ 2  4  6 56] 
  [ 8 10 12 89]]
 [[14 16 18 43] 
  [20 22 24 76]]]
                  
[[[20 22 24 76]]]

[[[ 2  4  6 56]
  [ 8 10 12 89]]]

[[[14 16 18 43]
  [20 22 24 76]]]
 
  
'''

 



answered Jan 25, 2019 by avibootz
edited Jan 27, 2019 by avibootz

Related questions

1 answer 68 views
2 answers 132 views
3 answers 217 views
1 answer 213 views
213 views asked Aug 28, 2016 by avibootz
...