python
  • sqlite3
  • iteration
  • pysqlite
  • frequency-distribution
  • 2012-06-11 2 views 0 likes 
    0

    내가 주파수 배포 데이터베이스 측 만들려고, 지금 약간의 시간이 작업을했습니다 :freq dist 계산을 위해 db 쿼리를 어떻게 효율적으로 처리합니까?

    from itertools import permutations 
    import sqlite3 
    
    def populate_character_probabilities(connection, table="freq_dist", alphabet='abcdefghijklmnopqrstuvwxyz'): 
        c = connection.cursor() 
        c.execute("DROP TABLE IF EXISTS tablename".replace('tablename', table)) 
        c.execute("create table tablename (char1 text, char2 text, freq integer);".replace("tablename", table))  
        char_freq_tuples = [x + (1,) for x in list(permutations(alphabet, 2)) + [(alpha, alpha) for alpha in alphabet + 'S' + 'E']] 
        c.executemany("insert into tablename values (?,?,?);".replace("tablename", table), char_freq_tuples) 
        connection.commit() 
        c.close() 
    
    def populate_word_list(connection, table="word_list"): 
        cursor = connection.cursor() 
        cursor.execute("DROP TABLE IF EXISTS tablename".replace('tablename', table)) 
        cursor.execute("create table tablename (word text);".replace('tablename', table)) 
        cursor.executemany("insert into tablename values (?)".replace('tablename', table), [[u'nabisco'], [u'usa'], [u'sharp'], [u'rise']]) 
        connection.commit() 
    
    def update_freq_dist(connection, word_list="word_list", freq_dist="freq_dist"): 
        cursor = connection.cursor() 
        subset = cursor.execute("select * from tablename;".replace("tablename", word_list)).fetchmany(5) 
        for elem in subset: # want to process e.g.: 5 at a time 
         elem = 'S' + elem[0] + 'E' # Start and end delimiters 
         for i in xrange(len(elem) - 1): 
          freq = cursor.execute("SELECT freq FROM tablename WHERE char1=? and char2=?;".replace("tablename", freq_dist), (elem[i], elem[i + 1])).fetchone() 
          cursor.execute("UPDATE tablename SET freq=? WHERE char1=? and char2=?;".replace("tablename", freq_dist), (freq + 1, elem[i], elem[i + 1])) 
        connection.commit() # seems inefficient having two queries here^ 
        cursor.close() 
    
    if __name__ == '__main__': 
        connection = sqlite3.connect('array.db') 
        populate_word_list(connection) 
        populate_character_probabilities(connection) 
        update_freq_dist(connection) 
        cursor = connection.cursor() 
        print cursor.execute("SELECT * FROM freq_dist;").fetchmany(10) 
    

    이 (와우, 아래 테스트 케이스 37-180 라인의 코드베이스 것을 얻었다을! : D는 - 실제 단어 목록이 2,900 만하지 4 않습니다!) 나는 것을 깨달았다

    :

    • 나는 update_freq_dist 내부 루프 내에서 두 개의 질의를 필요가 없습니다
    • 내가 하나 문제를 해결할 수있는 방법을 확실 해요 그러나 시간

    에서 예컨대 : 5 데이터베이스 요소 (행)을 반복하는 방법, 있습니다.

    해결책이 있습니까?

    답변

    1

    주파수를 +1하고 싶습니까?

    UPDATE tablename 
    SET freq = freq + 1 
    WHERE char1=? and char2=?; 
    

    또는 다른 테이블에서 업데이트하는 경우 :

    UPDATE tablename 
    SET freq = t2.freq + 1 -- whatever your calc is 
    FROM tablename t1 
    JOIN othertable t2 
    ON t1.other_id = t2.id 
    WHERE t1.char1=? and t1.char2=? and t2.char1=? and t2.char2=? 
    

    한 번에 5를 반복에 관해서는 - 당신이 가까운 무언가를 얻을 수 limit and offset 절을 사용할 수 있습니다.

     관련 문제

    • 관련 문제 없음^_^