Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is absolutely no question that this should be done using a database, rather than pickle- databases are designed for exactly this kind of problem.</p> <p>Here is some code to get you started, which puts a dictionary into a sqllite database and shows an example of retrieving a value. To get this to work with your actual dictionary rather than my toy example, you'll need to learn more about SQL, but fortunately there are many excellent resources available online. In particular, you might want to learn how to use <a href="http://www.sqlalchemy.org/">SQLAlchemy</a>, which is an "Object Relational Mapper" that can make working with databases as intuitive as working with objects.</p> <pre><code>import os import sqlite3 # an enormous dictionary too big to be stored in pickle my_huge_dictionary = {"A": 1, "B": 2, "C": 3, "D": 4} # create a database in the file my.db conn = sqlite3.connect('my.db') c = conn.cursor() # Create table with two columns: k and v (for key and value). Here your key # is assumed to be a string of length 10 or less, and your value is assumed # to be an integer. I'm sure this is NOT the structure of your dictionary; # you'll have to read into SQL data types c.execute(""" create table dictionary ( k char[10] NOT NULL, v integer NOT NULL, PRIMARY KEY (k)) """) # dump your enormous dictionary into a database. This will take a while for # your large dictionary, but you should do it only once, and then in the future # make changes to your database rather than to a pickled file. for k, v in my_huge_dictionary.items(): c.execute("insert into dictionary VALUES ('%s', %d)" % (k, v)) # retrieve a value from the database my_key = "A" c.execute("select v from dictionary where k == '%s'" % my_key) my_value = c.next()[0] print my_value </code></pre> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload