我想创建一个像这样的python数组:
use galaxy60_1000;
CREATE EXTERNAL TABLE scd_member1 (scd_member_key string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LOCATION '/user/AutomationFiles/galaxy60_1000/scd_member';
CREATE EXTERNAL TABLE scd_member3 (scd_member_key string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LOCATION '/user/AutomationFiles/galaxy60_1000/scd_member';
但是,我想使用一个函数来执行此操作,因此,如果数组的尺寸发生变化,我仍然会得到相同的输出,其中外部元素在数值上增加而中间元素保持为零。
答案 0 :(得分:5)
这是尝试使用N
和M
作为高度和宽度:
N = 5
M = 7
m = []
# first row
m.append([1] + list(range(2*N + 2*M - 4, M + 2*N - 3, -1)))
# middle rows
for i in range(1, N-1):
row = M*[0]
row[0] = i+1
row[-1] = 2*N + M -2 - i
m.append(row)
# last row
m.append(list(range(N, N+M)))
for row in m:
strgs = ('{:2d}'.format(n) for n in row)
print(' '.join(strgs))
它打印:
1 20 19 18 17 16 15
2 0 0 0 0 0 14
3 0 0 0 0 0 13
4 0 0 0 0 0 12
5 6 7 8 9 10 11
与numpy
中的要求相同
import numpy as np
m = np.zeros(shape=(N, M), dtype=int)
# first row
m[0] = [1] + list(range(2*N + 2*M - 4, M + 2*N - 3, -1))
# middle rows
for i, row in enumerate(m[1:-1], start=2):
row[0] = i
row[-1] = 2*N + M -1 - i
# last row
m[-1] = list(range(N, N + M))