Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

dict.update

 
Jump to:  
 
Mike P
PostPosted: Tue Sep 02, 2008 3:04 pm    Post subject: dict.update
       
Hi All,

I have two dictionaries e.g
dict1 = {123:3,234:5,456:3}
dict2 = {123:4,157:2,234:5,456:3,567:2}

I want to merge these two dictionaries together so i have a resultant
dictionary of:

dict3 = {123:[4,3],157:[2,0],234:[5,5],456:[3,3],567:[2,0]}

As later on i want to write a csv file that would have the form

id var1 var2
123 4 3
157 2 0

i looks like the dict.update looks almost there but i can't get it to
work properly, can anyone offer any advise?
 

 
Diez B. Roggisch
PostPosted: Tue Sep 02, 2008 3:04 pm    Post subject: Re: dict.update
       
Mike P schrieb:
Quote:
Hi All,

I have two dictionaries e.g
dict1 = {123:3,234:5,456:3}
dict2 = {123:4,157:2,234:5,456:3,567:2}

I want to merge these two dictionaries together so i have a resultant
dictionary of:

dict3 = {123:[4,3],157:[2,0],234:[5,5],456:[3,3],567:[2,0]}

As later on i want to write a csv file that would have the form

id var1 var2
123 4 3
157 2 0

i looks like the dict.update looks almost there but i can't get it to
work properly, can anyone offer any advise?


res = {}
for d in dict1, dict2:
for key, value in d.iteritems():
res.setdefault(key, []).append(value)


Diez
 

 
Tino Wildenhain
PostPosted: Tue Sep 02, 2008 3:04 pm    Post subject: Re: dict.update
       
Mike P wrote:
Quote:
Hi All,

I have two dictionaries e.g
dict1 = {123:3,234:5,456:3}
dict2 = {123:4,157:2,234:5,456:3,567:2}

I want to merge these two dictionaries together so i have a resultant
dictionary of:

dict3 = {123:[4,3],157:[2,0],234:[5,5],456:[3,3],567:[2,0]}

As later on i want to write a csv file that would have the form

id var1 var2
123 4 3
157 2 0

i looks like the dict.update looks almost there but i can't get it to
work properly, can anyone offer any advise?

result=dict((key,(dict1.get(key,None),
dict2.get(key,None)))
for key in set(dict1.keys()+dict2.keys())
)

(untested)

should provide you with a resulting dictonary with tuples
where [0] is var1 and [1] is var2, None for values not
in one of the dictionaries (you can put in 0 if you prefer).

You can use it like so to generate your output:

allitems=result.items()
allitems.sort() # sort based on keys as first tuple element

for (key,(var1,var2)) in allitems:
print key,var1,var2

HTH
Tino
 

 
Raymond Hettinger
PostPosted: Tue Sep 02, 2008 3:18 pm    Post subject: Re: dict.update
       
On Sep 2, 8:04 am, Mike P <michael.pearm...@tangozebra.com> wrote:
Quote:
Hi All,

I have two dictionaries e.g
dict1 = {123:3,234:5,456:3}
dict2 = {123:4,157:2,234:5,456:3,567:2}

I want to merge these two dictionaries together so i have a resultant
dictionary of:

dict3 = {123:[4,3],157:[2,0],234:[5,5],456:[3,3],567:[2,0]}

As later on i want to write a csv file that would have the form

id      var1  var2
123     4      3
157     2      0

i looks like the dict.update looks almost there but i can't get it to
work properly, can anyone offer any advise?

The update() method is not quite right for your purposes.
But a simple generator expression will do the trick:


Quote:
dict((k, [v, dict1.get(k, 0)]) for k, v in dict2.items())
{456: [3, 3], 234: [5, 5], 123: [4, 3], 157: [2, 0], 567: [2, 0]}



Raymond
 

 
Mike P
PostPosted: Tue Sep 02, 2008 3:28 pm    Post subject: Re: dict.update
       
Thanks Diez,

This is almost perfect!

Is there a way to ensure each list has two elements, even if one of
them is blank?

Mike
 

 
Mike P
PostPosted: Tue Sep 02, 2008 3:31 pm    Post subject: Re: dict.update
       
Thanks Raymond,

That's a neat trick, i'll look into learning more about this

Mike
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

katalog infoblogi drzwi teksty piosenek rusztowania