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

时间:2017-11-08 09:43:14

标签: python python-2.7 mysql-python

考虑一个表测试属性的表(id,test_case,file_name,coverage) 因为我将从列表中获取多个file_name作为输入,我只需要显示一次test_case属性值。这是下面的例子:

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)

因为我需要test_case属性值来打印唯一,即我不需要重复的test_case名称。 我只需要test_case名称一次,输出应如下所示:

(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)

1 个答案:

答案 0 :(得分:2)

您可以将已显示的值保存在集合中,以记住已打印的值,并跳过它们:

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)
相关问题