python 计算循环次数_Python-如何计数和记住循环中的出现次数

1586010002-jmsa.png

Hopefully i can explain as clear as possible of what i'm trying to do.

I'm running a while loop every 2 seconds to get updated values from 2 different stores that have different values:

the dictionaries looks like this:

#2 seconds

{'lastUpdateId': 202588846, 'store1': [['24.43000000', '0.00606000'], ['24.42000000', '14.76000000'], ['24.39000000', '2.65760000'], ['24.38000000', '29.59867000'], ['24.35000000', '7.71171000']], 'store2': [['24.47000000', '0.22601000'], ['24.52000000', '0.72000000'], ['24.53000000', '3.28839000'], ['24.54000000', '5.63226000'], ['24.55000000', '20.64052000']]}

#2 seconds

{'lastUpdateId': 202588904, 'store1': [['24.45000000', '0.22596000'], ['24.44000000', '12.84000000'], ['24.43000000', '22.43211000'], ['24.42000000', '5.87234000'], ['24.39000000', '2.65760000']], 'store2': [['24.51000000', '0.00003000'], ['24.52000000', '2.80979000'], ['24.53000000', '17.64938000'], ['24.67000000', '3.41000000'], ['24.68000000', '115.07610000']]}

Now what i want to do is compare inside of a loop the second value from store 1 to the second value of store 2, i want to count up to 10 times if 10 is counted then

i want it to continue to the next line of code, but this is where i'm stuck i don't how to do this, what can i do for python to remember the loop counts?

here is what i have tried so far:

import time

def testing():

stores = {'lastUpdateId': 202588846, 'store1': [['24.43000000', '0.00606000'], ['24.42000000', '14.76000000'], ['24.39000000', '2.65760000'], ['24.38000000', '29.59867000'], ['24.35000000', '7.71171000']], 'store2': [['24.47000000', '0.22601000'], ['24.52000000', '0.72000000'], ['24.53000000', '3.28839000'], ['24.54000000', '5.63226000'], ['24.55000000', '20.64052000']]}

firststore = [i[1] for i in stores['store1']]

secondstore = [e[1] for e in stores['store2']]

offer1 = float(firststore[0]) + float(firststore[1]) + float(firststore[2])

offer2 = float(secondstore[0]) + float(secondstore[1]) + float(secondstore[2])

count = 0

if offer1 > offer2:

count+=1

print("This store has the lesser better deal")

if count == 10:

print("go ahead and buy")

if offer2 > offer1:

count+=1

print("this other store has the lesser letter deal")

if count == 10:

print("go buy this")

i = 0

while True:

i+=1

testing()

time.sleep(2)

could provide me with a better idea? maybe with a for loop and append?

解决方案

What you're looking for is a generator. Here is a great explanation of generators and how they work.

I broke out your comparison code into a separate generator function, which is assigned to generator object offer_comparer by calling the generator function compare_offer. The the next call starts function execution and returns at the first yield.

Then, each call to offer_comparer.send() sends offer1 and offer2 to the offers variable in the generator function.

This should give you what you're looking for.

import time

def compare_offers():

count_max = 10

while True:

count1 = 0

count2 = 0

while count1 < count_max and count2 < count_max:

offers = yield

offer1, offer2 = offers[0], offers[1]

if offer1 > offer2:

count1 += 1

print("Store1 has the better deal")

if count1 == count_max:

print("go ahead and buy from store1")

elif offer2 > offer1:

count2 += 1

print("Store2 has the better deal")

if count2 == count_max:

print("go buy this from store2")

offer_comparer = compare_offers()

next(offer_comparer)

def testing():

stores = {'lastUpdateId': 202588846,

'store1': [['24.43000000', '0.00606000'],

['24.42000000', '14.76000000'],

['24.39000000', '2.65760000'],

['24.38000000', '29.59867000'],

['24.35000000', '7.71171000']],

'store2': [['24.47000000', '0.22601000'],

['24.52000000', '0.72000000'],

['24.53000000', '3.28839000'],

['24.54000000', '5.63226000'],

['24.55000000', '20.64052000']]}

firststore = [float(i[1]) for i in stores['store1']]

secondstore = [float(e[1]) for e in stores['store2']]

offer1 = sum(firststore[:3])

offer2 = sum(secondstore[:3])

offer_comparer.send((offer1, offer2))

i = 0

while True:

i += 1

testing()

time.sleep(2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值