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.. 9 23005
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.
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
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
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}
Abandoned a écrit :
>
I'm sorry my bed english.
Time to go to bad, then !-)
(sorry, couldn't resist)
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
>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
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
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()) This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by none |
last post: by
|
16 posts
views
Thread by Ling Lee |
last post: by
|
1 post
views
Thread by I am dog |
last post: by
|
1 post
views
Thread by john wright |
last post: by
|
2 posts
views
Thread by Benjamin Georgi |
last post: by
|
2 posts
views
Thread by Nagu |
last post: by
|
reply
views
Thread by Nagu |
last post: by
|
2 posts
views
Thread by slais-www |
last post: by
| | | | | | | | | | | |