我正在通過RFID閱讀器收到我的消息。我所試圖做的是消除重複和只添加到列表以下兩個條件:如何選擇附加列表?
- 如果他們的ID(
epc
在這種情況下)是獨一無二的,(完成) 的
datetime
(每5分鐘,所以我可以跟蹤相同的標籤仍然是由RFID閱讀器讀取)是間隔5分鐘後import paho.mqtt.client as mqtt import json testlist = [] def on_message(client, userdata, msg): payloadjson = json.loads(msg.payload.decode('utf-8')) line = payloadjson["value"].split(',') epc = line[1] datetime = payloadjson['datetime'] # datetime is in this string format '2016-04-06 03:21:17' payload = {'datetime': datetime, 'epc': epc[11:35]} # this if-statement satisfy condition 1 if payload not in testlist: testlist.append(payload) for each in teslist: print (each) test = mqtt.Client(protocol = mqtt.MQTTv31) test.connect(host=_host, port=1883, keepalive=60, bind_address="") test.on_connect = on_connect test.on_message = on_message test.loop_forever()
我怎麼能達到條件2?
UPDATE
我的目標不明確道歉,我試圖實現
我希望的輸出是這個樣子:
{'datetime': 2016-04-06 03:21:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:21:18', 'epc': 00000002} # from Tag B
...
...
# 5 minutes later
{'datetime': 2016-04-06 03:21:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:21:18', 'epc': 00000002} # from Tag B
{'datetime': 2016-04-06 03:26:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:26:18', 'epc': 00000002} # from Tag B
...
...
# Another 5 minutes later
{'datetime': 2016-04-06 03:21:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:21:18', 'epc': 00000002} # from Tag B
{'datetime': 2016-04-06 03:26:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:26:18', 'epc': 00000002} # from Tag B
{'datetime': 2016-04-06 03:31:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:31:18', 'epc': 00000002} # from Tag B
...
...
謝謝你的答案!請問'testlist ['epc']'和'testlist [epc]'有什麼區別?根據我目前的理解,前者僅僅意味着使用'epc'作爲dict'testlist'的關鍵字,那麼後者呢?背後有什麼「魔力」? –
''epc''是一個由三個字符'e','p'和'c'組成的字符串。 'testlist ['epc']'使用該文字字符串作爲鍵。相反,'epc'是一個變量名,可以綁定到任何值。例如,'epc = 42'將名稱'epc'綁定到值'42'。 'testlist [epc]'使用綁定到名字'epc'的值作爲鍵。 – RootTwo