仅保存 while 循环的最后一次迭代 [英] Only last iteration of while loop saves

查看:57
本文介绍了仅保存 while 循环的最后一次迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

symbolslist = ["100","200","300","400","500","600","700","800","900","1000","1500"、"2000"、"3000"、"4000"、"5000"、"7000"、"10000"]我=0而 i<len(symbolslist):htmltext = urllib.urlopen("http://www.fifacoinszone.com/default/quick/getpriceetail?platform_id=7&coins="+symbolslist[i] +"&cur=GBP")数据 = json.load(htmltext)pricelist = 数据["single_price_just"]打印价目表,我+=1

输出:

<预> <代码> 4.69 9.32 13.91 18.46 22.96 27.41 31.82 36.18 40.50 44.78 66.83 88.66 132.32 175.55 218.34 304.15 345.86 430.17 3.94 7.83 11.69 15.51 19.29 23.03 26.74 30.40 34.03 37.62 56.15 74.50 111.19 147.52 183.48 255.58 363.30

这很好,但是当我尝试将这段代码切割成更小的变量时,它不允许我这样做.例如,pricelist,[0:20] 将只输出 while 循环的最后一次迭代.抱歉,我是 Python 新手.

解决方案

您的 pricelist 变量在循环的每次迭代中都会被覆盖.您需要将结果存储在某种数据结构中,例如 list(并且 list 将与 [0:20]您希望使用的切片符号):

symbolslist = ["100","200","300","400","500","600","700","800","900","1000","1500"、"2000"、"3000"、"4000"、"5000"、"7000"、"10000"]pricelist = [] #空列表我=0而 i<len(symbolslist):htmltext = urllib.urlopen("http://www.fifacoinszone.com/default/quick/getpriceetail?platform_id=7&coins="+symbolslist[i] +"&cur=GBP")数据 = json.load(htmltext)pricelist.append(data["single_price_just"]) #将您的结果附加到列表的末尾print pricelist[i] #打印最近添加的价目表成员我+=1

现在你可以:

pricelist[0:20] #返回价目表的第 0 到 19 位成员

就像你想要的那样.

我还建议使用 for 循环,而不是在 while 循环中手动递增计数器.

Python 2:

 for i in xrange(len(symbolslist)):

Python 3:

for i in range(len(symbolslist)):#xrange 也适用于 Python 3,但它只是为了#提供向后兼容性.

如果你这样做,你可以省略最后的 i+=1 行.

I have this code:

symbolslist = ["100","200","300","400","500","600","700","800","900","1000","1500","2000","3000","4000","5000","7000","10000"]

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 = data["single_price_just"]
     print pricelist,
     i+=1

This outputs:

4.69 9.32 13.91 18.46 22.96 27.41 31.82 36.18 40.50 44.78 66.83 88.66 132.32 175.55 218.34 304.15 345.86 430.17 3.94 7.83 11.69 15.51 19.29 23.03 26.74 30.40 34.03 37.62 56.15 74.50 111.19 147.52 183.48 255.58 363.30

which is fine but when I try to then chop this code into smaller variables it doesn't let me. For example,pricelist,[0:20] will just output the last iteration of the while loop. Sorry I am new to Python.

解决方案

Your pricelist variable is being overwritten on each iteration of the loop. You need to store your result in a data structure of some sort, such as a list (and a list will work with the [0:20] slice notation you wish to use):

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

Now you can do:

pricelist[0:20] #returns members 0 to 19 of pricelist

Just like you wanted.

I also suggest using a for loop instead of manually incrementing your counter in a 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.

If you do it this way you can omit the i+=1 line at the end.

这篇关于仅保存 while 循环的最后一次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆