0

I'm new at coding in python and I was wondering if there is some way I can transcript this code in C to python:

for (K=1; K<3; K++)
    for (I=0; I<3; I++)
        for (J=0; J<7; J++){
            printf("Year: %d\tProvince: %d\tMonth: %d: ", K, I, J);
            scanf("%f", &A[I][J][K]);
        }

This is all I have done so far, the only thing missing is how to input data in the three dimensional array

for k in range(1,3):
    for i in range(1,3):
        for j in range(1,7):
            print("Year: " + str(k) + " Province: " + str(i) + " Month: " + str(j) + ": ")
  • 2
    Possible duplicate of [How do you read from stdin in Python?](http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python) – kaylum Aug 31 '16 at 23:21
  • If reading from stdin is not what you need to know then please be more specific in describing what it is that you don't understand how to do. – kaylum Aug 31 '16 at 23:21
  • You could preallocate a 3d list and populate it, but its not very pythonic and difficult to get right. I think you should probably just build the lists as you iterate and it will be far easier to reason about. – Paul Rooney Sep 01 '16 at 00:15

2 Answers2

0

you could use numpy. example assuming you want float64s. change types add needed

import numpy as np

arr = np.empty([2, 2, 6], np.float64)
for k in range(2):
    for i in range(2):
        for j in range(6):
            print("Year: " + str(k) + " Province: " + str(i) + " Month: " + str(j) + ": ")
            arr[k][i][j] = np.float64(raw_input())
Jules G.M.
  • 3,624
  • 1
  • 21
  • 35
0

If you want to do that in just standard, native python this should work:

import pprint # To display "3d array" in a recognizable format 
A = [[[0 for _ in range(2)] for _ in range(2)] for _ in range(6)]

for k in range(2):
    for i in range(2):
        for j in range(6):
            print("Year: {0} Province: {1} Month: {2}".format(k,i,j))
            A[j][i][k] = raw_input()

pprint.pprint(A) # for an easy to read 2x2x6 format display

After inputting some numbers (eg. 1-24), gives output:

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