# -*- coding: utf-8 -*- stock1={ 'stockName':"沈阳机床", 'stockCode':"000410", 'averagePrice_yesterday':34.08, 'averagePrice_today':35.49, 'position_old':0.0941350558312, } stock2={ 'stockName':"暴风科技", 'stockCode':"300431", 'averagePrice_yesterday':238.11, 'averagePrice_today':245.96, 'position_old': 0.0976148704234, } #字符串动态生成对象 eval()方法 for i in range(1,3): print eval('stock' + str(i))['stockName']+"最新仓位: ",eval('stock' + str(i))['position_old']
-------------------输出结果如下:---------------------------
沈阳机床最新仓位: 0.0941350558312
暴风科技最新仓位: 0.0976148704234