python - 将表表示为矩阵

标签 python database-design data-structures matrix

假设我有一个包含 3 个字段的数据库表:字符串标题、int A、int B。 A和B的取值范围均为1~500。
我想将部分值表示为 5x5 矩阵。 这样 (1, 1) 将是具有最低 A 和 B 的字符串; (5, 5) 将具有最高的 A 和 B; (1, 5) 将具有最低的 A 和最高的 B。依此类推。
我应该使用哪种算法?

最佳答案

我在这里设置了一个模拟,评论将描述这些步骤。

首先,我生成一些数据:一系列元组,每个元组包含一个字符串和两个代表分数 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

关于python - 将表表示为矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10257935/

相关文章:

python变量作为类

python - 从 numpy 类型获取位数的简单方法?

mysql - 需要更好的数据库设计方法

python - 如何遍历列表列表的列表列表...... python 中的一对坐标列表?

data-structures - 覆盖数据结构?

python - Pipenv "ModuleNotFoundError: No module named ' pip'"升级到python3.7后

Python Matplotlib 样式文件 : show only horizontal gridlines

mysql数据库链表反规范化

sql - 布线数据库的数据库方案

excel - 日期重叠 : generalized way to split the dates to get the overlapping interval?