-1
我有多塊代碼,我需要重複多次(按順序)。這裏有兩個塊的例子(還有更多)。多次重複python代碼 - 是否有凝結它的方法?
#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))
隨後
#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))
相反複製和粘貼這些多次,是有各塊冷凝成單個變量的方法嗎?我試過定義函數,但由於代碼塊沒有參數,所以沒有意義。
使用for循環? – DavidG
在python中是否有這樣的功能? – matpol
@matpol erm ...是? – jonrsharpe