[英]Efficient counting of word occurrences in Python
Suppose I have these two tables: 假设我有两个表:
Table1: 表格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 .....................................
Table2: 表2:
CODE DESCRIPTION
------------------
13A DISEASE1
13B DISEASE2
13C DISEASE3
13D DISEASE4
I want to find an efficient way of counting the code occurrences for each id and create count vectors based on the codes from the second table..For example: 我想找到一种有效的方法来计算每个id的代码出现次数,并根据第二张表中的代码创建计数向量。例如:
[2,0,0,1] represents the count vector for person with id=1, where each value is the occurrence of the code from table2 [2,0,0,1]代表id = 1的人的计数向量,其中每个值都是table2中代码的出现
I managed to do that in way but it looks like it is not very efficient...Is there a more efficient way? 我设法做到了这一点,但看起来效率不是很高……有没有更有效的方法?
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)]
Using generators will probably speed it up: 使用生成器可能会加快速度:
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)
You might want to iterate over table1
in chunks. 您可能想要对table1
进行大块迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.