Re: Multiple values for one key

2008-08-29 Thread Willi Richert
Hi,

try defaultdict:

In [1]: from collections import defaultdict

In [2]: d=defaultdict(list)

In [3]: d[1].append(7)

In [4]: d[1].append(8)

In [5]: d
Out[5]: defaultdict(type 'list', {1: [7, 8]})

In [6]: d[1]
Out[6]: [7, 8]

Regards,
wr

Am Donnerstag 28 August 2008 19:02:55 schrieb Ron Brennan:
 I have another question.

 How would like to be able to add the contents on the values for one key.

 key['20001']:[978, 345]

 How can I do this?

 Thanks,
 Ron

 On Thu, Aug 28, 2008 at 11:56 AM, Bruno Desthuilliers

 [EMAIL PROTECTED] wrote:
  norseman a é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:0
  girl: 123 tiny street:12345
  boy:321 here:3
  gal:999 over there: 5
  so forth
 
  Thus one key has many values. And you can then index on whatever key(s)
  you wish - name,zip...
 
  You can either use 1/ a list of dicts, or 2/ a dict mapping keys to
  lists.
 
  1/
  records = [
{name:guy, address:unknown,zip:0},
{name:girl, address:123 tiny street,zip:12345},
{name:boy, address:321 here,zip:3},
{name:gal, address:999 over there,zip:5},
  ]
 
  keys = (name, address, zip)
 
  print :.join(keys)
  print - * len(:.join(keys))
  for record in records:
 data = [record[key] for key in keys]
 print :.join(data)
 
 
  2/
  records = dict(
 name=[guy, girl, boy, gal],
 address=[unknown,123 tiny street,321 there,999 over there],
 zip=[0, 12345, 3, 5]
 )
 
  keys = (name, address, zip)
  nb_records = len(records[keys[0]])
 
  print :.join(keys)
  print - * len(:.join(keys))
  for i in xrange(nb_records):
 data = [data[key][i] for key in keys]
 print :.join(data)
 
 
  You are of course entitled the right to prefer the second solution, but
  then I hope I'll never have to maintain your code, since it's obviously
  not an appropriate data structure.
 
  With billions plus records,
 
 
  With billions plus records, it may be time to move to a serious RDBMS.
  Which btw will provide solution 1, or a lighter version of it using a
  list of tuples, ie:
 
  cursor = connection.cursor()
  cursor.execute(select name, address, zip from peoples)
  records = cursor.fetchall()
 
  # at this time, you have :
  #records = [
  #   (guy, unknown,0,),
  #   (girl, 123 tiny street,12345,),
  #   (boy, 321 here,3,),
  #   (gal, 999 over there, 5,),
  #]
 
 
  (snip)
 
  OK - I know I missed the whole concept of a Python Dictionary.
 
 
  Bad thing for you, since it's the central datastructure in Python.
 
  I haven't read anything as yet that gives a clear picture of what it is
  and
 
  what it is for.
 
  Then you failed to read the FineManual's tutorial, which is where you
  should have started:
 
  http://docs.python.org/tut/node7.html#SECTION00750
 
  Do yourself a favour : read the above first, then if you still have
  questions about dicts, we'll gladly try to help.
 
  And do yourself another favour : learn about SQL, relational model and
  RDBMS.
 
  (snip description of why the OP *really* wants a RDBMS)
 
  --
  http://mail.python.org/mailman/listinfo/python-list


--
http://mail.python.org/mailman/listinfo/python-list

Re: Multiple values for one key

2008-08-28 Thread Maric Michaud
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:0
 girl: 123 tiny street:12345
 boy:321 here:3
 gal:999 over there: 5
 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'VVV',
 
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
---
IntegrityErrorTraceback (most recent call last)

/home/maric/ipython console in module()

IntegrityError: columns name, address are not unique




 Steve
 [EMAIL PROTECTED]
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple values for one key

2008-08-28 Thread Bruno Desthuilliers

norseman a é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:0
girl: 123 tiny street:12345
boy:321 here:3
gal:999 over there: 5
so forth

Thus one key has many values. And you can then index on whatever key(s) 
you wish - name,zip...


You can either use 1/ a list of dicts, or 2/ a dict mapping keys to lists.

1/
records = [
   {name:guy, address:unknown,zip:0},
   {name:girl, address:123 tiny street,zip:12345},
   {name:boy, address:321 here,zip:3},
   {name:gal, address:999 over there,zip:5},
]

keys = (name, address, zip)

print :.join(keys)
print - * len(:.join(keys))
for record in records:
data = [record[key] for key in keys]
print :.join(data)


2/
records = dict(
name=[guy, girl, boy, gal],
address=[unknown,123 tiny street,321 there,999 over there],
zip=[0, 12345, 3, 5]
)

keys = (name, address, zip)
nb_records = len(records[keys[0]])

print :.join(keys)
print - * len(:.join(keys))
for i in xrange(nb_records):
data = [data[key][i] for key in keys]
print :.join(data)


You are of course entitled the right to prefer the second solution, but 
then I hope I'll never have to maintain your code, since it's obviously 
not an appropriate data structure.



With billions plus records,


With billions plus records, it may be time to move to a serious RDBMS. 
Which btw will provide solution 1, or a lighter version of it using a 
list of tuples, ie:


cursor = connection.cursor()
cursor.execute(select name, address, zip from peoples)
records = cursor.fetchall()

# at this time, you have :
#records = [
#   (guy, unknown,0,),
#   (girl, 123 tiny street,12345,),
#   (boy, 321 here,3,),
#   (gal, 999 over there, 5,),
#]


(snip)


OK - I know I missed the whole concept of a Python Dictionary.


Bad thing for you, since it's the central datastructure in Python.

I haven't 
read anything as yet that gives a clear picture of what it is and what 
it is for.


Then you failed to read the FineManual's tutorial, which is where you 
should have started:


http://docs.python.org/tut/node7.html#SECTION00750

Do yourself a favour : read the above first, then if you still have 
questions about dicts, we'll gladly try to help.


And do yourself another favour : learn about SQL, relational model and 
RDBMS.


(snip description of why the OP *really* wants a RDBMS)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple values for one key

2008-08-28 Thread Tim Rowe
2008/8/28 Bruno Desthuilliers [EMAIL PROTECTED]:

 You can either use 1/ a list of dicts, or 2/ a dict mapping keys to lists.

 1/
 records = [
   {name:guy, address:unknown,zip:0},
   {name:girl, address:123 tiny street,zip:12345},
   {name:boy, address:321 here,zip:3},
   {name:gal, address:999 over there,zip:5},
 ]

I think I'd make a class with members name, address, zip, etc. Then
make a list of instances of that class or (better for access) identify
a primary key -- what *is* unique about each entry -- and use that as
a dictionary key with class instances as values. Or use a DBMS.
Certainly the original poster should read up on database design, or
chances are he'll have a nightmare maintaining referential integrity.

-- 
Tim Rowe
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple values for one key

2008-08-28 Thread Ron Brennan
I have another question.

How would like to be able to add the contents on the values for one key.

key['20001']:[978, 345]

How can I do this?

Thanks,
Ron

On Thu, Aug 28, 2008 at 11:56 AM, Bruno Desthuilliers
[EMAIL PROTECTED] wrote:

 norseman a é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:0
 girl: 123 tiny street:12345
 boy:321 here:3
 gal:999 over there: 5
 so forth

 Thus one key has many values. And you can then index on whatever key(s)
 you wish - name,zip...


 You can either use 1/ a list of dicts, or 2/ a dict mapping keys to lists.

 1/
 records = [
   {name:guy, address:unknown,zip:0},
   {name:girl, address:123 tiny street,zip:12345},
   {name:boy, address:321 here,zip:3},
   {name:gal, address:999 over there,zip:5},
 ]

 keys = (name, address, zip)

 print :.join(keys)
 print - * len(:.join(keys))
 for record in records:
data = [record[key] for key in keys]
print :.join(data)


 2/
 records = dict(
name=[guy, girl, boy, gal],
address=[unknown,123 tiny street,321 there,999 over there],
zip=[0, 12345, 3, 5]
)

 keys = (name, address, zip)
 nb_records = len(records[keys[0]])

 print :.join(keys)
 print - * len(:.join(keys))
 for i in xrange(nb_records):
data = [data[key][i] for key in keys]
print :.join(data)


 You are of course entitled the right to prefer the second solution, but
 then I hope I'll never have to maintain your code, since it's obviously not
 an appropriate data structure.

 With billions plus records,


 With billions plus records, it may be time to move to a serious RDBMS.
 Which btw will provide solution 1, or a lighter version of it using a list
 of tuples, ie:

 cursor = connection.cursor()
 cursor.execute(select name, address, zip from peoples)
 records = cursor.fetchall()

 # at this time, you have :
 #records = [
 #   (guy, unknown,0,),
 #   (girl, 123 tiny street,12345,),
 #   (boy, 321 here,3,),
 #   (gal, 999 over there, 5,),
 #]


 (snip)

 OK - I know I missed the whole concept of a Python Dictionary.


 Bad thing for you, since it's the central datastructure in Python.

 I haven't read anything as yet that gives a clear picture of what it is and
 what it is for.


 Then you failed to read the FineManual's tutorial, which is where you
 should have started:

 http://docs.python.org/tut/node7.html#SECTION00750

 Do yourself a favour : read the above first, then if you still have
 questions about dicts, we'll gladly try to help.

 And do yourself another favour : learn about SQL, relational model and
 RDBMS.

 (snip description of why the OP *really* wants a RDBMS)

 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
FYI, my email address is changing. My rogers account will be deactivated
shortly.  From now on please use:
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list

Re: Multiple values for one key

2008-08-28 Thread Chris Rebert
On Thu, Aug 28, 2008 at 10:02 AM, Ron Brennan [EMAIL PROTECTED] wrote:
 I have another question.

 How would like to be able to add the contents on the values for one key.

 key['20001']:[978, 345]

I'm assuming that by this you meant:
assert the_dict['20001'] == [978, 345]


 How can I do this?

sum(the_dict['20001']) #= 1323

Regards,
Chris

Follow the path of the Iguana...
Rebertia: http://rebertia.com
Blog: http://blog.rebertia.com


 Thanks,
 Ron

 On Thu, Aug 28, 2008 at 11:56 AM, Bruno Desthuilliers
 [EMAIL PROTECTED] wrote:

 norseman a é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:0
 girl: 123 tiny street:12345
 boy:321 here:3
 gal:999 over there: 5
 so forth

 Thus one key has many values. And you can then index on whatever key(s)
 you wish - name,zip...

 You can either use 1/ a list of dicts, or 2/ a dict mapping keys to lists.

 1/
 records = [
   {name:guy, address:unknown,zip:0},
   {name:girl, address:123 tiny street,zip:12345},
   {name:boy, address:321 here,zip:3},
   {name:gal, address:999 over there,zip:5},
 ]

 keys = (name, address, zip)

 print :.join(keys)
 print - * len(:.join(keys))
 for record in records:
data = [record[key] for key in keys]
print :.join(data)


 2/
 records = dict(
name=[guy, girl, boy, gal],
address=[unknown,123 tiny street,321 there,999 over there],
zip=[0, 12345, 3, 5]
)

 keys = (name, address, zip)
 nb_records = len(records[keys[0]])

 print :.join(keys)
 print - * len(:.join(keys))
 for i in xrange(nb_records):
data = [data[key][i] for key in keys]
print :.join(data)


 You are of course entitled the right to prefer the second solution, but
 then I hope I'll never have to maintain your code, since it's obviously not
 an appropriate data structure.

 With billions plus records,

 With billions plus records, it may be time to move to a serious RDBMS.
 Which btw will provide solution 1, or a lighter version of it using a list
 of tuples, ie:

 cursor = connection.cursor()
 cursor.execute(select name, address, zip from peoples)
 records = cursor.fetchall()

 # at this time, you have :
 #records = [
 #   (guy, unknown,0,),
 #   (girl, 123 tiny street,12345,),
 #   (boy, 321 here,3,),
 #   (gal, 999 over there, 5,),
 #]


 (snip)

 OK - I know I missed the whole concept of a Python Dictionary.

 Bad thing for you, since it's the central datastructure in Python.

 I haven't read anything as yet that gives a clear picture of what it is
 and what it is for.

 Then you failed to read the FineManual's tutorial, which is where you
 should have started:

 http://docs.python.org/tut/node7.html#SECTION00750

 Do yourself a favour : read the above first, then if you still have
 questions about dicts, we'll gladly try to help.

 And do yourself another favour : learn about SQL, relational model and
 RDBMS.

 (snip description of why the OP *really* wants a RDBMS)
 --
 http://mail.python.org/mailman/listinfo/python-list



 --
 FYI, my email address is changing. My rogers account will be deactivated
 shortly.  From now on please use:
 [EMAIL PROTECTED]

 --
 http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list

Multiple values for one key

2008-08-27 Thread Ron Brennan
Hello,


How would I create a dictionary that contains multiple values for one key.
I'd also like the key to be able to have duplicate entries.

Thanks,
Ron

-- 
FYI, my email address is changing. My rogers account will be deactivated
shortly.  From now on please use:
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list

Re: Multiple values for one key

2008-08-27 Thread Fredrik Lundh

Ron Brennan wrote:

How would I create a dictionary that contains multiple values for one 
key.  I'd also like the key to be able to have duplicate entries.


look for the thread titled python multimap.

/F

--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple values for one key

2008-08-27 Thread Terry Reedy



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


Re: Multiple values for one key

2008-08-27 Thread Maric Michaud
Le Thursday 28 August 2008 01:56:54 Terry Reedy, vous avez écrit :
   I'd also like the key to be able to have duplicate entries.

 Dict keys must be hashable and unique.

Let's admit they don't have to be unique (it's easy to implement all sort of 
data structures in pure python), but what would be the syntax for modifying 
an existing value, for example, what should be the result of two consecutive 
assignements ?

d[5] = None
d[5] = 0
d[5] == [ None, 0 ] ??

What exactly are you trying to achieve ?

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple values for one key

2008-08-27 Thread norseman

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:0
girl: 123 tiny street:12345
boy:321 here:3
gal:999 over there: 5
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.


Steve
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list