你好美麗的人們!
我目前正在處理一個腳本,我試圖在 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/MEDIUM、MEDIUM -> 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)
?
uj5u.com熱心網友回復:
使用將股票字串映射到數字的字典。然后你可以檢查數值是否增加了。
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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/522050.html
上一篇:如何根據字典值總結字串行?
下一篇:資料框中的字典值
