私は複数のコードを複数回(順番に)繰り返す必要があります。ここに2つのブロックの例があります(さらに多くのブロックがあります)。複数回のPythonコードを繰り返します - それを凝縮する方法はありますか?
#wet spring
sprwetseq = [] #blank list
ran_yr = np.random.choice(wetspring,1) #choose a random year from the extreme winter variable
sprwetseq += [(ran_yr[0], 2)] #take the random year and the value '2' for spring to sample from
for item in sprwetseq: #item is a tuple with year and season, ***seq is all year and season pairs for the variable
projection.append(extremewet.query("Year == %d and Season == '%d'" % item))
の代わりにコピーし、これらを複数回貼り付け、続い
#cold winter
wincoldseq = [] #blank list
ran_yr = np.random.choice(coldwinter,1) #choose a random year from the extreme winter variable
wincoldseq += [(ran_yr[0], 1)] #take the random year and the value '1' for winter to sample from
for item in wincoldseq: #item is a tuple with year and season, ***seq is all year and season pairs for the variable
projection.append(extremecold.query("Year == %d and Season == '%d'" % item))
、単一の変数に各ブロックを凝縮する方法はありますか?私は関数を定義しようとしましたが、コードブロックに引数がないので意味がありませんでした。
は、forループを使用しますか? – DavidG
そこにPythonの関数として何かありますか? – matpol
@matpol erm ...はい? – jonrsharpe