0

假设我有这两个表:

表格1:

    ID   CODE         DATE        value1   value2   text
    -----------------------------------------------------
    1    13A       2012-05-04      12.0     0.0     null
    2    13B       2011-06-08      5.5      0.0     null
    3    13C       2012-07-05      4.0      0.0     null
    4    13D       2010-09-09      7.7      0.0     null
    1    13A       .....................................
    1    13D       .....................................
    3    13D       .....................................

表2:

    CODE  DESCRIPTION
    ------------------
    13A    DISEASE1
    13B    DISEASE2
    13C    DISEASE3
    13D    DISEASE4

我想找到一种有效的方法来计算每个 id 的代码出现次数,并根据第二个表中的代码创建计数向量。例如:

[2,0,0,1] 表示 id=1 的人的计数向量,其中每个值是表 2 中代码的出现次数

我设法做到了这一点,但看起来效率不高......有没有更有效的方法?

sql = "SELECT * FROM table1"
cursor.execute(sql)
table1 = cursor.fetchall()

sql2 = "SELECT CODE FROM table2"
cursor.execute(sql2)
codes = cursor.fetchall()

list1 = []
list2 = []
cnt = Counter()
countList = []
n=len(codes)

for id,iter in itertools.groupby(table1,operator.itemgetter('ID')):
    idList = list(iter)
    list1.append(list((z['CODE']) for z in idList))
for pat in list1:
    for code in codes: 
        cnt=pat.count(code.get('CODE'))
        list2.append(cnt)
countList = [list2[i:i+n] for i in range(0, len(list2), n)]
4

1 回答 1

0

使用生成器可能会加快速度:

import itertools
import operator

def code_counter(table, codes):
    for key, group in itertools.groupby(table, key=operator.itemgetter('ID')):
        group_codes = [item['CODE'] for item in group]

        yield [group_codes.count(code) for code in codes]

if __name__ == '__main__':
    cursor.execute("SELECT * FROM table1")
    table1 = cursor.fetchall()

    cursor.execute("SELECT CODE FROM table2")
    codes = [code.get('code') for code in cursor.fetchall()]

    for chunk in code_counter(table1, codes):
        print(chunk)

您可能希望分块进行迭代table1

于 2013-07-13T00:41:30.563 回答