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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,167 questions

40,724 answers

573 users

How to create a 3D list with random numbers in Python

2 Answers

0 votes
import pprint 
import random
  
def create_3D_list(d1, d2, d3): 
    lst = [[[random.randint(1, 30) for col in range(d1)] 
                                   for col in range(d2)] 
                                   for row in range(d3)] 
    return lst 
      

d1 = 6
d2 = 4
d3 = 3

lst = create_3D_list(d1, d2, d3)

pprint.pprint(lst) 




'''
run:
 
[[[10, 18, 7, 23, 22, 30],
  [1, 13, 16, 15, 25, 6],
  [13, 26, 22, 14, 3, 6],
  [29, 10, 1, 6, 10, 20]],
 [[11, 15, 2, 29, 5, 20],  
  [7, 22, 26, 30, 23, 3],
  [5, 26, 17, 9, 24, 24],  
  [10, 7, 12, 11, 21, 16]], 
 [[30, 17, 13, 3, 13, 17],
  [6, 29, 3, 19, 11, 30],
  [17, 22, 6, 2, 24, 16],  
  [4, 24, 8, 26, 7, 7]]]

'''

 





answered Feb 13, 2020 by avibootz
0 votes
import pprint 
import random
   
def create_3D_list(d1, d2, d3): 
    lst = [[[str(random.randint(1, 10)) for col in range(d1)] 
                                        for col in range(d2)] 
                                        for row in range(d3)] 
    return lst 
       
 
d1 = 6
d2 = 4
d3 = 3
 
lst = create_3D_list(d1, d2, d3)
 
pprint.pprint(lst) 
 
 
 
 
'''
run:
  
[[['8', '6', '2', '1', '4', '4'],
  ['10', '6', '10', '10', '4', '9'],
  ['7', '7', '7', '6', '4', '7'],
  ['7', '6', '5', '9', '2', '5']],
 [['3', '9', '7', '2', '9', '9'],  
  ['10', '2', '6', '9', '8', '10'],
  ['9', '1', '4', '4', '3', '5'],  
  ['2', '10', '4', '2', '2', '8']], 
 [['5', '4', '6', '3', '8', '10'],
  ['9', '9', '10', '10', '8', '2'],
  ['4', '7', '1', '9', '9', '8'],  
  ['8', '5', '2', '6', '7', '6']]]
 
'''

 





answered Feb 13, 2020 by avibootz

Related questions

1 answer 81 views
1 answer 71 views
71 views asked Oct 26, 2018 by avibootz
1 answer 60 views
...