python - Python - 如何计算和记住循环中的出现次数
问题描述
希望我能尽可能清楚地解释我正在尝试做的事情。
我每 2 秒运行一次 while 循环,以从 2 个具有不同值的不同商店获取更新的值:
字典看起来像这样:
#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']]}
现在我想要做的是在循环内比较商店 1 中的第二个值与商店 2 的第二个值,如果计数为 10,我想最多计数 10 次
我希望它继续到下一行代码,但这是我被卡住的地方我不知道该怎么做,我能做些什么让 python 记住循环计数?
这是我到目前为止所尝试的:
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)
能给我一个更好的主意吗?也许有一个for循环并追加?
解决方案
您正在寻找的是发电机。这是对生成器及其工作原理的一个很好的解释。
我将您的比较代码分解为一个单独的生成器函数,offer_comparer
通过调用生成器函数将其分配给生成器对象compare_offer
。next
调用开始函数执行并在第一个返回yield
。
然后,每次调用offer_comparer.send(<params>)
都会将 offer1 和 offer2 发送到offers
生成器函数中的变量。
这应该给你你正在寻找的东西。
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)
推荐阅读
- vb.net - 按钮和错误
- c# - 为什么我的 UI Button 在未单击时显示按钮单击消息?
- python - 在 pandas 数据帧上进行 Groupby,并根据列中值的频率用逗号连接字符串
- java - 线程内的 Spring @Transactional 支持
- excel - 保持执行VBA Excel的流程
- xml - 浏览 XmlNodeList (vb.net) 中的节点
- android - 如何从 webview 获取会话用户 ID
- node.js - socket.io 中默认的 pingTimeout 和 pingInterval 是什么
- c - 第二次输入后清洗管道
- vba - 添加表单控件时,全局变量莫名其妙地重置