Python 只需打印一次属性值,如果该属性值再次出现,则应忽略该属性值

Python 只需打印一次属性值,如果该属性值再次出现,则应忽略该属性值,python,python-2.7,mysql-python,Python,Python 2.7,Mysql Python,考虑一个测试属性的表(id、测试用例、文件名、覆盖率) 由于我将从列表中获取多个文件名作为输入,所以我只需要显示一次测试用例属性值。下面是一个例子: import MySQLdb out1=list() out1=['cdp.c',ndp_arp_fusen.c','discovery.c'] db=MySQLdb.connect(host="localhost",user="root",passwd="vinay123",db="test") cur=db.cursor() for line

考虑一个测试属性的表(id、测试用例、文件名、覆盖率) 由于我将从列表中获取多个文件名作为输入,所以我只需要显示一次测试用例属性值。下面是一个例子:

import MySQLdb
out1=list()
out1=['cdp.c',ndp_arp_fusen.c','discovery.c']
db=MySQLdb.connect(host="localhost",user="root",passwd="vinay123",db="test")
cur=db.cursor()
for line in out1:
    cur.execute("select * from check2 where file_name like %s",("%"+line))
    rows=cur.fetchall()
    for row in rows:
        print(row)
我得到的输出如下:

(3917L, 'test case1', 'cdp.c', 1L)
(7730L, 'test case2', 'cdp.c', 937L)
(9837L, 'test case3', 'cdp.c', 888L)
(11313L, 'test case4', 'cdp.c', 89L)
(15727L, 'test case5', 'cdp.c', 937L)
(19718L, 'test case6', 'cdp.c', 1L)
(25003L, 'test case7', 'cdp.c', 937L)
(25004L, 'test case7', 'cdp.c', 1L)
(25239L, 'test case8', 'cdp.c', 937L)
(25240L, 'test case8', 'cdp.c', 1L)
(3970L, 'test case1', 'ndp_arp_fusen.c', 81L)
(7780L, 'test case2', 'ndp_arp_fusen.c', 83L)
(15777L, 'test case5', 'ndp_arp_fusen.c', 83L)
(19771L, 'test case6', 'ndp_arp_fusen.c', 81L)
(25083L, 'test case7', 'ndp_arp_fusen.c', 83L)
(25084L, 'test case7', 'ndp_arp_fusen.c', 81L)
(3971L, 'test case1', 'discovery.c', 34L)
(7781L, 'test case2', '_discovery.c', 34L)
(9887L, 'test case3', 'discovery.c', 34L)
(10239L, 'test case4', 'discovery.c', 34L)
(15778L, 'test case5', 'discovery.c', 34L)
(19772L, 'test case6', 'discovery.c', 34L)
(25085L, 'test case7', 'discovery.c', 34L)
(25321L, 'test case8', 'discovery.c', 34L)
(3917L, 'test case1', 'cdp.c', 1L)
(7730L, 'test case2', 'cdp.c', 937L)
(9837L, 'test case3', 'cdp.c', 888L)
(11313L, 'test case4', 'cdp.c', 89L)
(15727L, 'test case5', 'cdp.c', 937L)
(19718L, 'test case6', 'cdp.c', 1L)
(25003L, 'test case7', 'cdp.c', 937L)
(25239L, 'test case8', 'cdp.c', 937L)
因为我需要测试用例属性值来打印唯一,也就是说我不需要重复的测试用例名称。 我只需要测试用例名一次,输出如下:

(3917L, 'test case1', 'cdp.c', 1L)
(7730L, 'test case2', 'cdp.c', 937L)
(9837L, 'test case3', 'cdp.c', 888L)
(11313L, 'test case4', 'cdp.c', 89L)
(15727L, 'test case5', 'cdp.c', 937L)
(19718L, 'test case6', 'cdp.c', 1L)
(25003L, 'test case7', 'cdp.c', 937L)
(25004L, 'test case7', 'cdp.c', 1L)
(25239L, 'test case8', 'cdp.c', 937L)
(25240L, 'test case8', 'cdp.c', 1L)
(3970L, 'test case1', 'ndp_arp_fusen.c', 81L)
(7780L, 'test case2', 'ndp_arp_fusen.c', 83L)
(15777L, 'test case5', 'ndp_arp_fusen.c', 83L)
(19771L, 'test case6', 'ndp_arp_fusen.c', 81L)
(25083L, 'test case7', 'ndp_arp_fusen.c', 83L)
(25084L, 'test case7', 'ndp_arp_fusen.c', 81L)
(3971L, 'test case1', 'discovery.c', 34L)
(7781L, 'test case2', '_discovery.c', 34L)
(9887L, 'test case3', 'discovery.c', 34L)
(10239L, 'test case4', 'discovery.c', 34L)
(15778L, 'test case5', 'discovery.c', 34L)
(19772L, 'test case6', 'discovery.c', 34L)
(25085L, 'test case7', 'discovery.c', 34L)
(25321L, 'test case8', 'discovery.c', 34L)
(3917L, 'test case1', 'cdp.c', 1L)
(7730L, 'test case2', 'cdp.c', 937L)
(9837L, 'test case3', 'cdp.c', 888L)
(11313L, 'test case4', 'cdp.c', 89L)
(15727L, 'test case5', 'cdp.c', 937L)
(19718L, 'test case6', 'cdp.c', 1L)
(25003L, 'test case7', 'cdp.c', 937L)
(25239L, 'test case8', 'cdp.c', 937L)

您可以将看到的值保存在一个集合中,以记住已打印的值,并跳过这些值:

seen = set()
for line in out1:
    cur.execute("select * from check2 where file_name like %s",("%"+line))
    rows=cur.fetchall()
    for row in rows:
        if row[1] in seen:
            continue
        else:
            seen.add(row[1])
            print(row)

不相关,但1/行
out1=list()
完全没有用处(它构建了一个空列表,在下一行被丢弃),2/您不需要调用
rows=cur.fetchall()
,您只需直接在光标上迭代(
for row in cur:…
),这将在数据集庞大时节省内存。酷,很高兴我能帮忙。请看