Anacondaを使用して、簡単なコードを書いています。私は配列がforループでどのように書き直されているか把握しようとしています。それは定義されています。私はボックスという名前の配列(ランダム0または1)を書いて、私は一人で残したい配列の「コピー」として新しい配列ホルダーを定義します。しかし、箱は書き直され続けており、どうやってこれをやるべきか分かりません。Pythonのnumpy配列がループで書き直されている - これをしないでください。
#FUNCTIONS
def initialize():
config = np.zeros([n, n])
for x in range(n):
for y in range(n):
if np.random.rand() < p:
config[x, y] = 1
return config
def roller(x):
test1 = np.roll(x,1,axis=0)
test2 = np.roll(x,1,axis=1)
test3 = np.roll(x,-1,axis=1)
test4 = np.roll(x,-1,axis=0)
hold = np.sum([test1,test2,test3,test4],axis=0)
return hold
def loop(steps,holder,store_config):
for t in range(steps):
tot = roller(holder)
for x in range(n):
for y in range(n):
if tot[x,y] >= 4:
holder[x,y] = 1
else:
holder[x,y] = 0
store_config[:,:,t] = holder
def func():
start = time.time()
time_steps = 20
store_config = np.zeros([n,n,time_steps])
loop(time_steps,holder,store_config)
end = time.time()
print(end - start, np.sum(store_config))
#CONSTANTS
n = 100
p = .2
box = initialize() #Array to leave ALONE
print(np.sum(box))
#Action
holder = box #Array to manipulate and redefine as box
func()
print(np.sum(box))
はあなたの場合はnp.sum(ボックス)の出力からの値が走っている()の前と後のFUNCと一致する必要がありますが、彼らは決してしません。その意図は、func()を再実行すると値が吐き出されるが、同じ定義された "box"配列を反復するだけだが、書き直され続けるということである。私はその可能性を見ていない。私は配列がグローバルではない関数の中で変数のように扱われていると思いましたか? #Functions、#Constant、#Actionの3つのセクションのそれぞれは、Condaノートブックの独自のセルになります。
'ホルダー[X、Y] = '1'、'ホルダー[X、Y] = 0 'をあなたのループに。 – Evey