473,789 Members | 2,368 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 23079
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
3592
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 will contain instances of Item that are keyed off of the Item name. In __main__ I create two...
16
2073
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
2291
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
9263
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
2224
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
4513
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 description of the error:
0
1784
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
1448
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 random lookup would be more convenient. A solution based on AVL trees seems possible.
1
2278
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 where I an sending a dictionary object. In GetDictionaryType() I want to find out which dictionary...
0
9663
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10195
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9016
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6765
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.