473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Many-to-many pattern possiable?

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.

May 19 '07 #1
4 1824
"Jia Lu" <Ro*****@gmail.comwrote in message
news:11********************@p77g2000hsh.googlegrou 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
May 19 '07 #2
On May 19, 9:33 am, Jia Lu <Roka...@gmail.comwrote:
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

May 19 '07 #3
On May 19, 10:33 am, Jia Lu <Roka...@gmail.comwrote:
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?

May 19 '07 #4
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.
May 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
1
by: Raghuraman | last post by:
Hi i heared that Oracle can convert from number to word thru query i remember thru (jsp format) .Is it possiable in sqlserver Thanks
2
by: Raghuraman | last post by:
Hi, I've written a M.plan in my client place which is scheduled to occur weekly once.But 4 days gone from the last backup taken , there is a problem in the server which does not start loading...
9
by: Raghuram | last post by:
I dont have any application development tool like Visual Studios .NET or any other tools. .. i have only IIS Server and 1.1 framework installed with these resources can write a sample aspx page...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
11
by: td0g03 | last post by:
Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like for(i=1;i<=size;i) ...
1
by: halekio | last post by:
Hi all, Please bear with me as I've only started programming in C# 2 weeks ago and this is my first contact with OOP. I ran into a situation where I needed to catch an event in an object that...
3
by: Fuugie | last post by:
Is there an easier way to reverse a given pattern than the way I have coded it? Example of the program: Enter a DNA sequence: CGATTGAATTACAAGTCCAATT Enter the pattern: TGAA Mutated DNA...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.