1
當我使用這個腳本來獲取多少時間在數組中的每個元素重複陣列中的BI得到正確的結果比較數組的值使用Python
A = ['text1','text2','text3','text4']
B = ['text1','text2','text3','text4','text1','text2','text3','text4','text1','text2','text3','text4','text1','text2','text3','text4','text1','text2','text3','text4','text3','text4']
for c in A :
countVar = 0
for e in B :
if c == e :
countVar +=1
print c
print countVar
但是,當從MySQL數據庫返回在這種情況下,B數組是一個SQL查詢的返回結果,在這種情況下,確實爲第一個elemt拉出正確的數字是text1,並且它爲其他元素計數拉0。
cursor = db.cursor()
cursor.execute("select * from Table")
A = ['text1','text2','text3','text4']
for element in A:
#print element
for row in cursor.fetchall() :
if row[0] == element :
#Count
countClient += 1
print row
print countClient
countClient = 0
的結果是這樣的:知道我有文本2和文字3在我的表
的text1
文本2
文字3
文本4
任何一個知道這是爲什麼發生?或者提出另一種方式來做到這一點?我還在做其他計算,比如計算從SQL數組返回的值的總和,以及AVG,因爲我正在處理某些數據。
在此先感謝!
謝謝你很多回復大衛!!!你是對的,它不是第二次循環,你能否提出另一種方法來取代'fetchall()'?再次感謝! – mongotop 2013-03-06 03:20:05
查看我的最新編輯。這比試圖在評論塊中擠壓它容易得多。 – 2013-03-06 03:25:17
你是對的,我只是試圖修改你的答案:)我剛剛想到的,它的作品完美!你真棒戴維!非常感謝!!!! – mongotop 2013-03-06 03:27:35