Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:14:52+00:00 2026-06-05T14:14:52+00:00

I’ve been working on this for a bit of time now, trying to create

  • 0

I’ve been working on this for a bit of time now, trying to create a frequency distribution database side:

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)

(Wow, got that 180 line codebase down to 37 for a test-case! 😀 – Note that the actual word list is 29 million not 4!!!)

I realised that:

  • I shouldn’t need two queries within the update_freq_dist inner loop
  • There is a way of iterating through the database elements (rows), e.g.: 5 at a time

However I’m unsure how I can solve either problem.

Can you think of a solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:14:55+00:00Added an answer on June 5, 2026 at 2:14 pm

    Do you just want to update the frequency with a + 1?

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

    Or, if you’re updating from another table:

    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=?
    

    As for iterating 5 at a time – you can use limit and offset clauses to get something close.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.