473,382 Members | 1,512 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,382 software developers and data experts.

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 23063
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(dict2) # 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...@uniqsys.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.sourceforge.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...@uniqsys.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.sourceforge.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(intersection(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**********************@57g2000hsv.googlegroups. 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
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...
16
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...
1
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
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...
2
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...
2
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...
0
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...
2
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...
1
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)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.