Python if语句匹配元组列表 - 删除重复项 [英] Python if statement to match lists of tuples - removing duplicates

查看:134
本文介绍了Python if语句匹配元组列表 - 删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以10秒为间隔从MySQL数据库中提取数据。由于每秒传入的数据量是可变的,我计划每隔10秒拉最近的50行。我的第一个函数运行良好,并适当地提取数据。

我的第二个函数应该删除上一次pull的重复项,并且只将unique推送给新的变量,但是我遇到一个错误,不知道如何解决它。这似乎是我的if语句,我不允许匹配每个元组的所有元素。

 导入mysql.connector 
导入日期时间
导入请求操作员输入
itemgetter
导入时间

run = True

def sqlPull():
connection = mysql.connector.connect (用户='XXXX',密码='XXXXX',主机='XXXXXX',数据库='MeshliumDB')
cursor = connection.cursor()
cursor.execute(SELECT TimeStamp,MAC, RSSI FROM wifiscan ORDER BY TimeStamp DESC LIMIT 50;)
data = cursor.fetchall()
connection.close()
time.sleep(10)


$ b

数据输出如下所示:

 <$ c $ (104346,datetime.datetime(2013,11,14,17,43,20),u'00:1E:4C:03:C0:66',u'16'),(104345,datetime.datetime (2013,11,14,17,43,20),u'00:1E:4C:03:C0:66',u'16'),(104344,datetime.datetime(2013,11,14,17, (U)00:26:AB:BB:FB:B8',U'17'),(104343,datetime.datetime(2013, 11,14,17,43,18),u'8C:2D:AA:42:0E:67',u'17'),(104342,datetime.datetime(2013,11,14,17,43,17 ),u'E4:CE:8F:3F:36:E0',u'13'),(104341,datetime.datetime(2013,11,14,17,43,16),u'00:1E:8F :75:82:35',u'9'),(104340,datetime.datetime(2013,11,14,17,43,16),u'00:1E:8F:75:82:35',u '9'),(104339,datetime.datetime(2013,11,14,17,43,15),u'74:44:01:31:D9:E2',u'9'),(104338,datetime .datetime(2013,11,14,17,43,19),u'84:38:35:5E:F8:BA',u'8'),(104337,datetime.datetime(2013,11,14, 17:43,15),u'00:26:08:E2:38:F9',u'8')] 




def dupCatch ():
lastPull = []
for TimeStamp,MAC,RSSI in data:
如果TimeStamp,MAC,RSSI不在lastPull
data = newData
else
lastPull = data

while run == True:
sqlPull()
dupCatch()
print newData



文件< ipython- input-1-6d9d5eb980c9>,第20行
如果TimeStamp,MAC,RSSI不在lastPull
^
语法错误:语法无效


解决方案

这本来是一个评论,但它似乎有帮助,所以我将其重新发布为答案: p>

逗号语法告诉python创建一个元组,是的。这就是为什么以下两种行为是相同的:

  L = [1,2,3] 
a,b, c = L

  L = [1,2,3] 
(a,b,c)= L

然而,我怀疑这并不总是适用,例如在if语句或循环中表达条件的情况下。这可能表示这些表达式不是Python语法本身的一部分;但不用担心,因为语法可以在源代码中编辑(我知道这是可能的事实,但引用现在转义了我),随后可以将它编译为您自己的python版本。 p>

然而,暂时你想强制这些变量被收集到一个元组中,这样你就可以检查该元组中是否存在该元组。为此,请尝试以下操作:

  if(TimeStamp,MAC,RSSI)不在lastPull中:

编辑:我检查了语法,这看起来确实如此。


I am attempting to pull data from MySQL database on a 10 second interval. Since the amount of data coming in every second is variable, I plan on pulling the most recent 50 rows every 10 seconds. My first function works well and pulls the data appropriately.

My second function is supposed to remove duplicates from the previous pull and only push uniques to the new variable, but I am encountering an error and not sure how to fix it. It seems like for my if statement I am not allowed to match all elements of each tuple. I'm not really sure how to change this.

import mysql.connector
import datetime
import requests
from operator import itemgetter
import time

run = True

def sqlPull():
    connection = mysql.connector.connect(user='XXXX', password='XXXXX', host='XXXXXX', database='MeshliumDB')
    cursor = connection.cursor()
    cursor.execute("SELECT TimeStamp, MAC, RSSI FROM wifiscan ORDER BY TimeStamp DESC LIMIT 50;")
    data = cursor.fetchall()
    connection.close()
    time.sleep(10)

The data output looks like this:

[(104346, datetime.datetime(2013, 11, 14, 17, 43, 20), u'00:1E:4C:03:C0:66', u' 16'), (104345, datetime.datetime(2013, 11, 14, 17, 43, 20), u'00:1E:4C:03:C0:66', u' 16'), (104344, datetime.datetime(2013, 11, 14, 17, 43, 19), u'00:26:AB:BB:FB:B8', u' 17'), (104343, datetime.datetime(2013, 11, 14, 17, 43, 18), u'8C:2D:AA:42:0E:67', u' 17'), (104342, datetime.datetime(2013, 11, 14, 17, 43, 17), u'E4:CE:8F:3F:36:E0', u' 13'), (104341, datetime.datetime(2013, 11, 14, 17, 43, 16), u'00:1E:8F:75:82:35', u' 9'), (104340, datetime.datetime(2013, 11, 14, 17, 43, 16), u'00:1E:8F:75:82:35', u' 9'), (104339, datetime.datetime(2013, 11, 14, 17, 43, 15), u'74:44:01:31:D9:E2', u' 9'), (104338, datetime.datetime(2013, 11, 14, 17, 43, 19), u'84:38:35:5E:F8:BA', u' 8'), (104337, datetime.datetime(2013, 11, 14, 17, 43, 15), u'00:26:08:E2:38:F9', u' 8')]




def dupCatch():
    lastPull = []
    for TimeStamp, MAC, RSSI in data:
        if TimeStamp, MAC, RSSI not in lastPull
            data = newData
            else
            lastPull = data

while run == True:
    sqlPull()
    dupCatch()
    print newData



  File "<ipython-input-1-6d9d5eb980c9>", line 20
    if TimeStamp, MAC, RSSI not in lastPull
                ^
SyntaxError: invalid syntax

解决方案

This was originally a comment, but it seemed to be helpful, so I'm reposting it as an answer:

The comma syntax tells python to create a tuple, yes. This is why the following two behaviors are identical:

L = [1, 2, 3]
a, b, c = L

and

L = [1, 2, 3]
(a, b, c) = L

However, I suspect that this doesn't always apply, for instance in the case of expressing a condition in an if-statement or a loop. This might be indicative of such expressions not being part of the python grammar itself; but not to worry, as the grammar can be edited in the source code (I know for a fact that this is possible, but the citation escapes me at the moment), which can subsequently be compiled into your own flavor of python.

For the time being, however, you want to force those variables to be gathered into a tuple, so that you can check for the existence of that tuple in the set. To that end, try this:

if (TimeStamp, MAC, RSSI) not in lastPull:
  # do stuff

EDIT: I checked the grammar and this does really seem to be the case.

这篇关于Python if语句匹配元组列表 - 删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆