考虑一个表测试,其中属性为(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)
最佳答案
您可以将看到的值保存在一组中以记住已打印的值并跳过它们:
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)
关于python - 仅需要打印一次属性值,如果该属性值再次出现,则应忽略该属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47176271/