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. 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
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
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?
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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
|
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...
|
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...
|
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...
|
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)
...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
| |