美丽的人们好!
我目前正在编写一个脚本,每当dict值增加时,我都会尝试打印出来,并使用以下数据示例打印出来:
First request
{
'00194953243062': {
'value': '00194953243062',
'stock': 'OOS',
'modificationDate': '2022-10-22T12:02:06.000Z'
},
'00194953243086': {
'value': '00194953243086',
'stock': 'OOS',
'modificationDate': '2022-09-30T10:55:45.000Z'
},
'00194953243093': {
'value': '00194953243093',
'stock': 'OOS',
'modificationDate': '2022-10-22T11:05:54.000Z'
},
'00194953243130': {
'value': '00194953243130',
'stock': 'OOS',
'modificationDate': '2022-10-22T08:55:48.000Z'
}
}
print("All values are OOS!")
****************************************************************************************************
Second request
{
'00194953243062': {
'value': '00194953243062',
'stock': 'OOS',
'modificationDate': '2022-10-22T12:02:06.000Z'
},
'00194953243086': {
'value': '00194953243086',
'stock': 'OOS',
'modificationDate': '2022-09-30T10:55:45.000Z'
},
'00194953243093': {
'value': '00194953243093',
'stock': 'OOS',
'modificationDate': '2022-10-22T11:05:54.000Z'
},
'00194953243130': {
'value': '00194953243130',
'stock': 'MEDIUM',
'modificationDate': '2022-10-22T08:55:48.000Z'
}
}
print("New value has been found!")
****************************************************************************************************
Third request
{
'00194953243062': {
'value': '00194953243062',
'stock': 'LOW',
'modificationDate': '2022-10-22T12:02:06.000Z'
},
'00194953243086': {
'value': '00194953243086',
'stock': 'OOS',
'modificationDate': '2022-09-30T10:55:45.000Z'
},
'00194953243093': {
'value': '00194953243093',
'stock': 'OOS',
'modificationDate': '2022-10-22T11:05:54.000Z'
},
'00194953243130': {
'value': '00194953243130',
'stock': 'OOS',
'modificationDate': '2022-10-22T08:55:48.000Z'
}
}
print("New value has been found!")
****************************************************************************************************
Forth request
{
'00194953243062': {
'value': '00194953243062',
'stock': 'OOS',
'modificationDate': '2022-10-22T12:02:06.000Z'
},
'00194953243086': {
'value': '00194953243086',
'stock': 'OOS',
'modificationDate': '2022-09-30T10:55:45.000Z'
},
'00194953243093': {
'value': '00194953243093',
'stock': 'OOS',
'modificationDate': '2022-10-22T11:05:54.000Z'
},
'00194953243130': {
'value': '00194953243130',
'stock': 'OOS',
'modificationDate': '2022-10-22T08:55:48.000Z'
}
}
print("All values are OOS!")
这些都是可能发生的每个请求的例子,在另一个stackoverflow线程的帮助下,我成功地完成了这样的事情:
previous_data = {}
gtin = # Is the example I have given above
if previous_data == gtin:
# if our keys are the same we can check which values have changed based on your logic
if all(value['stock'].casefold() == 'oos' for att, value in gtin.items()):
print("All values are OOS!")
#only if they have changed to low, medium or high
elif any(
(value['stock'].casefold() in ['low', 'medium', 'high'] and
previous_data[att]['stock'].casefold() == 'oos')
for att, value in gtin.items()):
print("New value has been found!")
previous_data = gtin
它目前解决的问题是,每当股票价值从OOS变为低/中/高时,它都会打印出有一个正确的变化:
if OOS -> LOW/MEDIUM/HIGH -> Print
if LOW -> MEDIUM/HIGH -> Print
if MEDIUM -> HIGH/LOW -> Print
if HIGH -> LOW/MEDIUM -> Print
但问题是,我不想在HIGH时打印->OOS/低/中,中->LOW/OOS和LOW->OOS。
我的目标是获得预期结果:
预期结果:
if OOS -> LOW/MEDIUM/HIGH -> Print
if LOW -> MEDIUM/HIGH -> Print
if MEDIUM -> HIGH -> Print
if LOW -> OOS - **dont** print (print if all stock are oos)
if MEDIUM -> OOS/LOW- **dont** print (print if all stock are oos)
if HIGH -> OOS/LOW/MEDIUM - **dont** print (print if all stock are oos)
if ALL values are OOS -> print All OOS
对我来说,如果这些值中的任何一个增加,那么我们就不必检查其他值,只要其中一个场景是真的。
我的问题是,当这种情况发生时,我如何用我的代码在没有打印出来的地方获得预期的结果:
if LOW -> OOS - **dont** print (print if all stock are oos)
if MEDIUM -> OOS/LOW- **dont** print (print if all stock are oos)
if HIGH -> OOS/LOW/MEDIUM - **dont** print (print if all stock are oos)
使用一个将股票字符串映射到数字的字典。然后您可以检查数值是否增加。
stock_map = {
'oos': 0,
'low': 1,
'medium': 2,
'high': 3
}
...
elif (any(stock_map[value['stock'].casefold()] > stock_map[previous_data[att]['stock'].casefold()]
for att, value in gtin.items()):
print("New value has been found")