我是python编码的新手,我想知道是否有某种方法可以将这段代码用C转录成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]);
}
这是我到目前为止所做的全部,唯一缺少的是如何在三维数组中输入数据
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) + ": ")
答案 0 :(得分:0)
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())
答案 1 :(得分:0)
如果你想在标准的原生python中这样做,这应该有效:
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
输入一些数字(例如1-24)后,输出:
[[['1', '13'], ['7', '19']],
[['2', '14'], ['8', '20']],
[['3', '15'], ['9', '21']],
[['4', '16'], ['10', '22']],
[['5', '17'], ['11', '23']],
[['6', '18'], ['12', '24']]]