Python 批量生成变量 发表于 2016-09-22 | 分类于 Python 基本思想就是利用locals()字典生成特定的变量,如列表,字典等。 123456789for p,i in enumerate(range(1,11)): locals()['l%i'%(p+1)] = []for i in range(1,11): locals()['list'+str(i)] = [] for i in range(1,11): a='list%d=[]'%i exec a 另小九九 1234567dict_ = {}for i in xrange(1,10): dict_[i]='' for j in range(1,i+1): tmp = str(j)+'*'+str(i)+'='+str(j*i) dict_[i] = dict_[i]+' '+tmp print dict_[i]