473,574 Members | 4,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combine two dictionary...

Hi..
dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element

I want to combine dict1 and dict2 and i don't want to use FOR because
i need to performance.

I'm sorry my bed english.
King regards..

Oct 1 '07 #1
9 23070
On Mon, 01 Oct 2007 10:24:39 -0700, Abandoned wrote:
Hi..
dict1={1: 4, 3: 5}... and 2 millions element dict2={3: 3, 8: 6}... and
3 millions element

I want to combine dict1 and dict2 and i don't want to use FOR because i
need to performance.

I'm sorry my bed english.
King regards..
If mutation of one dict is okay, use the `update` method. As in::

dict1.update(di ct2) # will update dict1 with dict2's items

Create a copy of one dictionary if you do not want to mutate them.
See http://docs.python.org/lib/typesmapping.html#l2h-294 for details.
Oct 1 '07 #2
On Mon, 2007-10-01 at 10:24 -0700, Abandoned wrote:
Hi..
dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element

I want to combine dict1 and dict2 and i don't want to use FOR because
i need to performance.
You'll have to be a bit more precise here about how and where you want
to "combine". Should the result be a new dictionary or should it replace
one of dict1 or dict2? You're also not saying how you want conflicts
between dict1 and dict2 should be resolved. For example, both dict1 and
dict2 say something about the key 3. One says 5, the other says 3.
Should the combined dict say 3 or 5 or (3,5) or maybe even 4?

Also, based on your earlier posts from today, I'm wondering if those
dicts come from executing SQL queries. If so, you might want to consider
combining the results in SQL (using a UNION query) instead of combining
them in Python.

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 1 '07 #3
On Oct 1, 10:24 am, Abandoned <best...@gmail. comwrote:
Hi..
dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element

I want to combine dict1 and dict2 and i don't want to use FOR because
i need to performance.
The dict.update approach is the fastest way to build a combined
dictionary; however, it may be much cheaper to logically combine the
dictionaries rather than spending the time and memory to build a
physically combined dictionary: http://aspn.activestate.com/ASPN/Coo.../Recipe/305268

Raymond

Oct 1 '07 #4
On 1 Ekim, 20:41, Carsten Haese <cars...@uniqsy s.comwrote:
On Mon, 2007-10-01 at 10:24 -0700, Abandoned wrote:
Hi..
dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element
I want to combine dict1 and dict2 and i don't want to use FOR because
i need to performance.

You'll have to be a bit more precise here about how and where you want
to "combine". Should the result be a new dictionary or should it replace
one of dict1 or dict2? You're also not saying how you want conflicts
between dict1 and dict2 should be resolved. For example, both dict1 and
dict2 say something about the key 3. One says 5, the other says 3.
Should the combined dict say 3 or 5 or (3,5) or maybe even 4?

Also, based on your earlier posts from today, I'm wondering if those
dicts come from executing SQL queries. If so, you might want to consider
combining the results in SQL (using a UNION query) instead of combining
them in Python.

--
Carsten Haesehttp://informixdb.sour ceforge.net
I want to total score..
For example
dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element
result should be dict3={1:4, 3:8, 8:6}

Oct 1 '07 #5
Abandoned a écrit :
>
I'm sorry my bed english.
Time to go to bad, then !-)

(sorry, couldn't resist)

Oct 1 '07 #6
Abandoned wrote:
On 1 Ekim, 20:41, Carsten Haese <cars...@uniqsy s.comwrote:
>On Mon, 2007-10-01 at 10:24 -0700, Abandoned wrote:
>>Hi..
dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element
I want to combine dict1 and dict2 and i don't want to use FOR because
i need to performance.
You'll have to be a bit more precise here about how and where you want
to "combine". Should the result be a new dictionary or should it replace
one of dict1 or dict2? You're also not saying how you want conflicts
between dict1 and dict2 should be resolved. For example, both dict1 and
dict2 say something about the key 3. One says 5, the other says 3.
Should the combined dict say 3 or 5 or (3,5) or maybe even 4?

Also, based on your earlier posts from today, I'm wondering if those
dicts come from executing SQL queries. If so, you might want to consider
combining the results in SQL (using a UNION query) instead of combining
them in Python.

--
Carsten Haesehttp://informixdb.sour ceforge.net

I want to total score..
For example
>>dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element

result should be dict3={1:4, 3:8, 8:6}
Well not sure how this will work with 5+ million elements, but here's
one stab at it:
>>dict1={1: 4, 3: 5}
dict2={3: 3, 8: 6}
for key in set(dict1.keys( ) + dict2.keys()):
... x = dict1.get(key, 0)
... y = dict2.get(key, 0)
... dict3[key] = x + y
...
>>dict3
{8: 6, 1: 4, 3: 8}

As Carsten Haese pointed out this should really be done in SQL. You
might try something along these lines:

SELECT
t1.id AS id,
NVL(t1.count, 0) + NVL(t2.count, 0) AS count
FROM DUAL
LEFT JOIN (
SELECT
id,
count
FROM table1
) AS t1 ON 1=1
LEFT JOIN (
SELECT
id,
count
FROM table2
) AS t2 ON t1.id = t2.id

NOTE: I've never actually used a UNION, but you might want to look into it.

Ian
Oct 1 '07 #7
>I want to total score..
>For example
>>>dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element
result should be dict3={1:4, 3:8, 8:6}

Well not sure how this will work with 5+ million elements, but here's
one stab at it:
>>dict1={1: 4, 3: 5}
>>dict2={3: 3, 8: 6}
>>for key in set(dict1.keys( ) + dict2.keys()):
... x = dict1.get(key, 0)
... y = dict2.get(key, 0)
... dict3[key] = x + y
...
>>dict3
{8: 6, 1: 4, 3: 8}
Or if doing it in-place is sufficient, you can use

for k,v in dict2.iteritems ():
dict1[k] = dict1.get(k, 0) + v

Or, if you like stupid iterator/comprehension tricks

dict1.update(
(k,dict1.get(k, 0) + v)
for k,v in dict2.iteritems ()
)

-tkc

Oct 1 '07 #8
On Oct 1, 2:01 pm, Abandoned <best...@gmail. comwrote:
I want to total score..
For example
dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element

result should be dict3={1:4, 3:8, 8:6}
Unless you have some prior knowledge about the dicts (e.g.
len(intersectio n(d1,d2))), it's hard to beat the following, in pure
Python at least:

def sumdicts(d1,d2) :
if len(d1) < len(d2):
d1,d2 = d2,d1
dsum = dict(d1)
for key,value in d2.iteritems():
if key not in dsum:
dsum[key] = value
else:
dsum[key] += value
return dsum

Surprisingly (?), this turns out to be faster than using dict.get(key,
0) instead of the explicit if/else.

George

Oct 2 '07 #9
In message <11************ **********@57g2 000hsv.googlegr oups.com>, Abandoned
wrote:
I want to total score..
For example
dict1={1: 4, 3: 5}... and 2 millions element
dict2={3: 3, 8: 6}... and 3 millions element

result should be dict3={1:4, 3:8, 8:6}
Don't people like one-liners? :)

dict((k, dict1.get(k, 0) + dict2.get(k, 0))
for k in dict1.keys() + dict2.keys())
Oct 5 '07 #10

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

Similar topics

1
3578
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148 respectively In the sample code you see that I have class Item and class Dict class Dict contains a dictionary called items. The items dictionary...
16
2049
by: Ling Lee | last post by:
Hello. I'm trying to write a small program that lets you put in a number as an integer and then it tells you the textuel representation of the number. Like if your input is 42, it will say four two. I found out that I have to make a dictionary like this: List = { 1:"one", 2:"two" and so on )
1
2282
by: I am dog | last post by:
Hi , I use VS 2005 beta1 to do some thing as follows. declare a Dictionary<ulong,DelegateTyep> dictionary = new ... and use dictionary object such as dictionary += aDelegateTyepObject;
1
9243
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
2
2220
by: Benjamin Georgi | last post by:
Hello list, I could use some help extracting the keys/values of a list of dictionaries from a string that is just the str() representation of the list (the problem is related to some flat file format I'm using for file IO). Example: ', 1: }, {0: , 1: , 2: }, {0: }]'
2
4499
by: Nagu | last post by:
I am trying to save a dictionary of size 65000X50 to a local file and I get the memory error problem. How do I go about resolving this? Is there way to partition the pickle object and combine later if this is a problem due to limited resources (memory) on the machine (it is 32 bit machine Win XP, with 4GB RAM). Here is the detail...
0
1727
by: Nagu | last post by:
I am trying to save a dictionary of size 65000X50 to a local file and I get the memory error problem. How do I go about resolving this? Is there way to partition the pickle object and combine later if this is a problem due to limited resources (memory) on the machine (it is 32 bit machine Win XP, with 4GB RAM). Please advice. Thank you,
2
1436
by: slais-www | last post by:
Before I set out to reinvent the wheel. I want a dictionary-like structure for which most lookups will be on a key that is not in the "dictionary"; in which case I want a key/value returned which is the closest key that is less than the search term. The batch solution of matching the values in two sorted sequences is efficient but a...
1
2270
by: sachin2 | last post by:
I am using 3 types of dictionaries. 1) Dictionary<string, string > d = new Dictionary<string, string>(); 2) Dictionary<string, List<string>> d = new Dictionary<string, List<string>>(); 3) Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>(); Now I am using GetDictionaryType Function...
0
7826
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8085
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8126
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6491
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5328
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1090
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.