Many-to-many pattern possiable?

Discussion in 'Python' started by Jia Lu, May 19, 2007.

  1. Jia Lu

    Jia Lu Guest

    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.
     
    Jia Lu, May 19, 2007
    #1
    1. Advertising

  2. "Jia Lu" <> wrote in message
    news:...

    > 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
     
    Gre7g Luterman, May 19, 2007
    #2
    1. Advertising

  3. On May 19, 9:33 am, Jia Lu <> 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
     
    Raymond Hettinger, May 19, 2007
    #3
  4. Jia Lu

    7stud Guest

    On May 19, 10:33 am, Jia Lu <> 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?
     
    7stud, May 19, 2007
    #4
  5. 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.
     
    Bruno Desthuilliers, May 20, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raghu Raman

    Auto messages possiable ?

    Raghu Raman, Jan 11, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    357
    Raghu Raman
    Jan 12, 2005
  2. Raghuram

    Re: Is it possiable

    Raghuram, Aug 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    415
  3. Scott Phelps
    Replies:
    14
    Views:
    691
    jan V
    Sep 8, 2005
  4. =?Utf-8?B?QmVha2Vy?=

    Is it possiable to create an access rule for a file?

    =?Utf-8?B?QmVha2Vy?=, Mar 20, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    370
    Jeff T
    Mar 20, 2007
  5. Raghu Raman

    Application/Cookies possiable in asp.net ocx page

    Raghu Raman, Feb 2, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    132
    Raghu Raman
    Feb 2, 2005
Loading...

Share This Page