Re: [Tutor] merging dictionary values based on key

2009-03-12 Thread Martin Walsh
> greg whittier wrote:
>> On Thu, Mar 12, 2009 at 4:24 PM, ski  wrote:
>> mylist = [{'a': 'x123', 'b':'12'}, {'a': 'x234', 'b': 'd33', 'c':
>> 'a23'}, {'a': 'x234', 'c': 'XX123'}  ]
>>> where mylist has nth number of dictionaries and i want to merge the
>>> values
>>> of the keys that are the same?
>>>
>>
>> If I understand what you mean by merging, I  think you want
>>
>> mylist = [{'a': 'x123', 'b':'12'}, {'a': 'x234', 'b': 'd33', 'c':
>> 'a23'}, {'a': 'x234', 'c': 'XX123'}  ]
>> merged_dict = {}
>> for dictionary in mylist:
>> for key, value in dictionary.items():
>> merged_dict.setdefault(key,[]).append(value)

Or similarly with defaultdict:

from collections import defaultdict

merged_dict = defaultdict(list)
for d in mylist:
for k, v in d.items():
merged_dict[k].append(v)

ski wrote:
> how would you do this for a specific key instead of all the keys?

alist = [d['a'] for d in mylist if d.has_key('a')]

HTH,
Marty
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] merging dictionary values based on key

2009-03-12 Thread ski

how would you do this for a specific key instead of all the keys?

greg whittier wrote:

On Thu, Mar 12, 2009 at 4:24 PM, ski  wrote:

Hello,
I have this issue, which I am unsure on how to solve.


mylist1 = {'a': 'x123', 'b':'12'}
mylist2 = {'a': 'x234', 'c': 'a23'}
for k in mylist2:

... if k in mylist1:
... mylist1[k] = [mylist1[k], mylist2[k]]
... else:
... mylist1[k] = mylist2[k]
...

mylist1

{'a': ['x123', 'x234'], 'c': 'a23', 'b': '12'}
this merges the two dictionaries, but what should be the method if:


mylist = [{'a': 'x123', 'b':'12'}, {'a': 'x234', 'b': 'd33', 'c':
'a23'}, {'a': 'x234', 'c': 'XX123'}  ]

where mylist has nth number of dictionaries and i want to merge the values
of the keys that are the same?

Thanks

Norman


If I understand what you mean by merging, I  think you want

mylist = [{'a': 'x123', 'b':'12'}, {'a': 'x234', 'b': 'd33', 'c':
'a23'}, {'a': 'x234', 'c': 'XX123'}  ]
merged_dict = {}
for dictionary in mylist:
for key, value in dictionary.items():
merged_dict.setdefault(key,[]).append(value)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] merging dictionary values based on key

2009-03-12 Thread greg whittier
On Thu, Mar 12, 2009 at 4:24 PM, ski  wrote:
> Hello,
> I have this issue, which I am unsure on how to solve.
>
 mylist1 = {'a': 'x123', 'b':'12'}
 mylist2 = {'a': 'x234', 'c': 'a23'}
 for k in mylist2:
> ...     if k in mylist1:
> ...             mylist1[k] = [mylist1[k], mylist2[k]]
> ...     else:
> ...             mylist1[k] = mylist2[k]
> ...
 mylist1
> {'a': ['x123', 'x234'], 'c': 'a23', 'b': '12'}

>
> this merges the two dictionaries, but what should be the method if:
>
 mylist = [{'a': 'x123', 'b':'12'}, {'a': 'x234', 'b': 'd33', 'c':
 'a23'}, {'a': 'x234', 'c': 'XX123'}  ]
>
> where mylist has nth number of dictionaries and i want to merge the values
> of the keys that are the same?
>
> Thanks
>
> Norman

If I understand what you mean by merging, I  think you want

mylist = [{'a': 'x123', 'b':'12'}, {'a': 'x234', 'b': 'd33', 'c':
'a23'}, {'a': 'x234', 'c': 'XX123'}  ]
merged_dict = {}
for dictionary in mylist:
for key, value in dictionary.items():
merged_dict.setdefault(key,[]).append(value)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] merging dictionary values based on key

2009-03-12 Thread ski

Hello,
I have this issue, which I am unsure on how to solve.

>>> mylist1 = {'a': 'x123', 'b':'12'}
>>> mylist2 = {'a': 'x234', 'c': 'a23'}
>>> for k in mylist2:
... if k in mylist1:
... mylist1[k] = [mylist1[k], mylist2[k]]
... else:
... mylist1[k] = mylist2[k]
...
>>> mylist1
{'a': ['x123', 'x234'], 'c': 'a23', 'b': '12'}
>>>

this merges the two dictionaries, but what should be the method if:

>>> mylist = [{'a': 'x123', 'b':'12'}, {'a': 'x234', 'b': 'd33', 'c': 
'a23'}, {'a': 'x234', 'c': 'XX123'}  ]


where mylist has nth number of dictionaries and i want to merge the 
values of the keys that are the same?


Thanks

Norman











___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor