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

2024-06-01 12:33:31 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑一个表测试,其中属性是(id、test\u case、file\u name、coverage) 因为我将从一个列表中获取多个文件名作为输入,所以我只需要显示一次testcase属性值。下面是一个例子:

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\u case属性值来打印unique,也就是说我不需要重复的test\u 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)

Tags: test属性discoverycasearpcdpcase2out1
1条回答
网友
1楼 · 发布于 2024-06-01 12:33:31

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

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)

相关问题 更多 >