Your pricelist
变量在循环的每次迭代中都被覆盖。您需要将结果存储在某种数据结构中,例如list
(and a list
将与[0:20]
您希望使用的切片符号):
symbolslist = ["100","200","300","400","500","600","700","800","900","1000","1500","2000","3000","4000","5000","7000","10000"]
pricelist = [] #empty list
i=0
while i<len(symbolslist):
htmltext = urllib.urlopen("http://www.fifacoinszone.com/default/quick/getpricedetail?platform_id=7&coins="+symbolslist[i] +"&cur=GBP")
data = json.load(htmltext)
pricelist.append(data["single_price_just"]) #appends your result to end of the list
print pricelist[i] #prints the most recently added member of pricelist
i+=1
现在你可以这样做:
pricelist[0:20] #returns members 0 to 19 of pricelist
就像你想要的那样。
我还建议使用for
循环而不是手动增加计数器while
loop.
Python 2:
for i in xrange(len(symbolslist)):
Python 3:
for i in range(len(symbolslist)):
#xrange will also work in Python 3, but it's just there to
#provide backward compatibility.
如果您这样做,您可以省略i+=1
线在最后。