Multiple values for one key

Maric Michaud maric at aristote.info
Thu Aug 28 10:31:50 EDT 2008


Le Thursday 28 August 2008 03:43:16 norseman, vous avez écrit :
> Terry Reedy wrote:
> > Ron Brennan wrote:
> >> Hello,
> >>
> >>
> >> How would I create a dictionary that contains multiple values for one
> >> key.
> >
> > Make the value a collection object (set or list if you plan to add and
> > delete).
> >
> >>  I'd also like the key to be able to have duplicate entries.
> >
> > Dict keys must be hashable and unique.
> >
> > tjr
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
>
> ================
> First part I understand, second is still giving me a problem.
>
> For some reason I still want keys to be dbf column headers.
> like:
>
> name:address:zip so forth
> ---- ------- --- ------------------
> guy: unknown:00000
> girl: 123 tiny street:12345
> boy:321 here:33333
> gal:999 over there: 55555
> so forth
>
> Thus one key has many values. And you can then index on whatever key(s)
> you wish - name,zip...
>
> With billions plus records, trying to put a unique key on each entry
> seems to preclude the need for a dictionary, just use the entries.
> (Format to SDF and index on substr(line,1,24)+substr(line,48,+5) etc..)
>                                    name        +         zip
> OK - I know I missed the whole concept of a Python Dictionary. I haven't
> read anything as yet that gives a clear picture of what it is and what
> it is for.  Please, classroom concepts usually consist of a handful of
> objects. I have files here that takes up multiple DVDs each, AFTER a 15
> to 1 compression. 3 bytes per pixel. I need to work with them. Things
> like changing geobase projections and cookie cutting based on real world
> coordinates and modifying the lumens and so forth. Based on what I've
> read, the Python Dictionary concept flat will not work for such as this.
> Yes - the example is overkill. But in my world it is reality. I deal
> with sizeable things. Not all are raster. Most all are binary!  Things
> that work on massive text files - like banking and mortgage - simply
> don't work here. There are seldom 'lines'. There are always bytes. Lots
> of bytes. Things that form a group are not necessarily stored sequentially.
>
> Back to What Is A Python Dictionary: Somebody please enlighten me.
>


Disctionaries are hash tables with a unique key and constant time lookup. What 
you want could be implemented as a complex data structures with as many dict 
as needed keys, but it seems you really want a relational table and a rdbms. 
This is exactly what they are for. A short example with the new python2.5 
sqlite package :

>>>[107]: import sqlite3

>>>[108]: from string import letters

>>>[109]: db = sqlite3.connect(':memory:')

>>>[110]: db.execute("create table 'table1' ('name' text(20), 'address' 
text(100), primary key ('name', 'address'))")
...[110]: <sqlite3.Cursor object at 0x2b0cd9712c38>

>>>[111]: db.executemany("insert into 'table1' values (?, ?)", 
((letters[i%len(letters)]*i, "%d street" % i) for i in range(1000))).rowcount
...[111]: 1000

>>>[112]: for i in db.execute("select * from 'table1' where address 
like '99 %'") : print i
   .....:
(u'VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV', 
u'99 street')

>>>[113]: for i in db.execute("select * from 'table1' where name 
like '___'") : print i
   .....:
(u'ddd', u'3 street')

>>>[114]: db.execute("insert into 'table1' values (?, ?)", ('ddd', '4 
street')).rowcount
...[114]: 1

>>>[115]: for i in db.execute("select * from 'table1' where name 
like '___'") : print i
   .....:
(u'ddd', u'3 street')
(u'ddd', u'4 street')

>>>[116]: db.execute("insert into 'table1' values (?, ?)", ('ddd', '4 
street')).rowcount
---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)

/home/maric/<ipython console> in <module>()

IntegrityError: columns name, address are not unique



>
> Steve
> norseman at hughes.net
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
_____________

Maric Michaud



More information about the Python-list mailing list