Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Many-to-many pattern possiable?

Reply
Thread Tools

Many-to-many pattern possiable?

 
 
Jia Lu
Guest
Posts: n/a
 
      05-19-2007
Hi all

I see dict type can do 1-to-1 pattern, But is there any method to do
1-to-many, many-to-1 and many-to-many pattern ? What about using some
Serialized objects?

Thanks.

 
Reply With Quote
 
 
 
 
Gre7g Luterman
Guest
Posts: n/a
 
      05-19-2007
"Jia Lu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...

> I see dict type can do 1-to-1 pattern, But is there any method to do
> 1-to-many, many-to-1 and many-to-many pattern ?


Dict objects can do many-to-1 relationships.

Dict[Key1] = Obj
Dict[Key2] = Obj
Dict[Key3] = Obj

1-to-many relationships are more tricky and not something built-in to
dictionaries. You can make each value in the dictionary a list of
associated values:

Dict[Key] = [Obj1, Obj2, Obj3]

You can even subclass dict to give it the members you like, for example:

class OneToMany(dict):
def Add(self, Index, Value):
V = self.get(Index, [])
V.append(Value)
self[Index] = V


 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      05-19-2007
On May 19, 9:33 am, Jia Lu <(E-Mail Removed)> wrote:
> I see dict type can do 1-to-1 pattern, But is there any method to do
> 1-to-many, many-to-1 and many-to-many pattern ?


>>> mm = {'a': ['A', 'B', 'C'], 'c': ['C', 'D', 'E'], 'b': ['A', 'D']}


>>> # Now, invert the relation
>>> mmr = {}
>>> for k, seq in mm.items():

.... for elem in seq:
.... mmr.setdefault(elem, []).append(k)
>>> mmr

{'A': ['a', 'b'], 'C': ['a', 'c'], 'B': ['a'], 'E': ['c'], 'D': ['c',
'b']}


> What about using some
> Serialized objects?


from pickle import loads, dumps
d = dict(a=dumps(someobj), b=dumps(anotherobj))
obj = loads(d['a'])


Raymond

 
Reply With Quote
 
7stud
Guest
Posts: n/a
 
      05-19-2007
On May 19, 10:33 am, Jia Lu <(E-Mail Removed)> wrote:
> Hi all
>
> I see dict type can do 1-to-1 pattern, But is there any method to do
> 1-to-many, many-to-1 and many-to-many pattern ?


How about:

one_to_many = {"a":[10, "red", 2.5]}

many_to_1 = {("red", 2.5, 3):"hello"}

many_to_many = {("red", 2.5, 3):[10, 20, 30]}

In reality, everything is mapped 1-1. I wonder if there is a computer
language where that isn't the case?

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      05-20-2007
Jia Lu a écrit :
> Hi all
>
> I see dict type can do 1-to-1 pattern, But is there any method to do
> 1-to-many,


a dict of lists

> many-to-1


What's the difference with 1-n ?

> and many-to-many pattern ?


As usual, using an intermediate dict.

> What about using some
> Serialized objects?


What for ?

Anyway, if you're after a relational model, better to use some
relational db, possibly with SQLAlchemy on top.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:26 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:25 PM
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern " jones9413@yahoo.com C++ 1 08-31-2007 04:09 AM
documents related to factory design pattern and Abstract foctory pattern. sunny C++ 1 12-07-2006 04:26 AM
boolean endsWith(String s, Pattern pattern) lepikhin@gmail.com Java 17 11-16-2005 10:31 AM



Advertisments