[Tutor] Question on List of Dict
Sunil Tech
sunil.techspk at gmail.com
Fri Sep 19 09:42:00 CEST 2014
Danny,
i wrote a method called *merge *below
can you be little clear with an example
I wrote something like this
ids = []
for i in tes:
if i['a'] not in ids:
ids.append(i['a'])
print ids
def merge(ids, tes):
for jj in ids:
txt = ''
for i in tes:
if i['a'] == jj:
txt = txt + ', ' + i['b']
i['b'] = txt
return tes
pprint.pprint(merge(ids, tes))
result is like
[1, 2]
[{'a': 1, 'b': ', this', 'c': 221},
{'a': 2, 'b': ', this', 'c': 215},
{'a': 1, 'b': ', this, is', 'c': 875},
{'a': 1, 'b': ', this, is, sentence', 'c': 874},
{'a': 2, 'b': ', this, another', 'c': 754},
{'a': 2, 'b': ', this, another, word', 'c': 745}]
from this result need to take off the other dict so that it'll match the
result_tes = [{'a': 1, 'b': 'this, is, sentence', 'c': '221, 875, 874'},
{'a': 2, 'b': 'this, another, word', 'c': '215, 754, 744'}]
On Fri, Sep 19, 2014 at 12:58 PM, Danny Yoo <dyoo at hashcollision.org> wrote:
>
> > {'a': 2, 'b': 'another', 'c': 754},
> > {'a': 2, 'b': 'word', 'c': 745}
> >
>
> > if the value of the 'a' is same, then all those other values of the dict
> should be merged/clubbed.
>
> Can you write a function that takes two of these and merges them? Assume
> that they have the same 'a'. Can you write such a function?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140919/b2d75c03/attachment.html>
More information about the Tutor
mailing list