2012-04-21 88 views
2

假設我有一個包含3個字段的數據庫表:字符串標題,int A,int B. A和B的範圍是1到500.
我想表示一部分值作爲矩陣5x5。 因此,(1,1)將是A和B都最低的字符串; (5,5)將具有最高的A和B; (1,5)將具有最低的A和最高的B等等。
我應該使用哪種算法?將表格表示爲矩陣

+0

你可以舉例說明你想做什麼嗎?我想我還沒有完全理解這個問題。 – 2012-04-21 10:12:47

回答

1

我在這裏設置了一個模擬,並且評論將會描述這些步驟。

首先我生成一些數據:一個系列中的每個包含字符串和表示得分A和B.

接着兩個隨機數我分A的範圍和B成五個相等間隔的箱元組,每一個都表示小區的最小值和最大值。

然後我連續查詢數據集以提取每個單元格中的字符串。

根據您使用的實際數據結構和存儲,有一百種優化方法。

from random import random 

# Generate data and keep record of scores 
data = [] 
a_list = [] 
b_list = [] 
for i in range(50): 
    a = int(random()*500)+1 
    b = int(random()*500)+1 
    rec = { 's' : 's%s' % i, 
      'a' : a, 
      'b' : b 
      } 
    a_list.append(a) 
    b_list.append(b) 
    data.append(rec) 

# divide A and B ranges into five bins 

def make_bins(f_list): 
    f_min = min(f_list) 
    f_max = max(f_list) 
    f_step_size = (f_max - f_min)/5.0 
    f_steps = [ (f_min + i * f_step_size, 
       f_min + (i+1) * f_step_size) 
       for i in range(5) ] 
    # adjust top bin to be just larger than maximum 
    top = f_steps[4] 
    f_steps[4] = (top[0], f_max+1) 
    return f_steps 

a_steps = make_bins(a_list) 
b_steps = make_bins(b_list) 

# collect the strings that fit into any of the bins 
# thus all the strings in cell[4,3] of your matrix 
# would fit these conditions: 
# string would have a Score A that is 
# greater than or equal to the first element in a_steps[3] 
# AND less than the second element in a_steps[3] 
# AND it would have a Score B that is 
# greater than or equal to the first element in b_steps[2] 
# AND less than the second element in a_steps[2] 
# NOTE: there is a need to adjust the pointers due to 
#  the way you have numbered the cells of your matrix 

def query_matrix(ptr_a, ptr_b): 
    ptr_a -= 1 
    from_a = a_steps[ptr_a][0] 
    to_a = a_steps[ptr_a][1] 

    ptr_b -= 1 
    from_b = b_steps[ptr_b][0] 
    to_b = b_steps[ptr_b][1] 

    results = [] 
    for rec in data: 
     s = rec['s'] 
     a = rec['a'] 
     b = rec['b'] 
     if (a >= from_a and 
      a < to_a and 
      b >= from_b and 
      b < to_b): 
      results.append(s) 
    return results 

# Print out the results for a visual check 
total = 0 
for i in range(5): 
    for j in range(5): 
     print '=' * 80 
     print 'Cell: ', i+1, j+1, ' contains: ', 
     hits = query_matrix(i+1,j+1) 
     total += len(hits) 
     print hits 
print '=' * 80 
print 'Total number of strings found: ', total 
+0

非常感謝你的配合。您爲我節省了大量時間 – Oceinic 2012-04-21 11:23:45

+0

Thanks @ php5engineer - 我確定您注意到這是一個簡單的原型,我相信您不希望查詢您的數據庫25次,滿足每次填充矩陣的需要。但也許這是一個單獨討論的主題:)享受! – gauden 2012-04-21 11:32:27

1

你有

title A B 
one 1 1 
two 1 2 
three 2 1 
four 3 3 
five 4 4 
six 5 5 
seven 5 1 
eight 1 5 

等等...?

減少到3×3矩陣,它會看起來像

a/b 1  2 3 
1 one two eight  
2 three four ? 
3 seven ? six 

的問題是,什麼應(2,2)點?平均?好的,並在一個5x5的矩陣? 您的定義缺少一些信息。

爲矩陣的算法將是:

  1. A和B計算最小值,最大值,平均值
  2. 問數據庫元組(AMIN,BMIN),(AAVG,BMIN),(λ最大,BMIN)等
  3. 填充值到矩陣

添加上:如果有不匹配,嘗試最小,最大和平均範圍。