[Tutor] Question on List of Dict

Sunil Tech sunil.techspk at gmail.com
Fri Sep 19 15:14:18 CEST 2014


Thank you Peter Otten,

actually i should study about the collections and defaultdict like how and
where these can be used and its advantage.



On Fri, Sep 19, 2014 at 5:59 PM, Peter Otten <__peter__ at web.de> wrote:

> Sunil Tech wrote:
>
> > Danny i did it like this
> >
> > result_dict = {}
> > for i in tes:
> >     if i['a'] in result_dict:
> >         temp = result_dict[i['a']]
> >         temp['b'].append(i['b'])
> >         temp['c'].append(i['c'])
> >         temp['a'] = i['a']
> >         result_dict[i['a']] = temp
> >     else:
> >         result_dict[i['a']] = {
> >             'b': [i['b']],
> >             'c': [i['c']],
> >             'a': i['a']}
> > pprint.pprint(result_dict.values())
> >
> > result is
> >
> > [{'a': 1, 'b': ['this', 'is', 'sentence'], 'c': [221, 875, 874]},
> >  {'a': 2, 'b': ['this', 'another', 'word'], 'c': [215, 754, 745]}]
> >
> > any can one improve this method in terms of performance, etc..
>
> What you have is a good solution; the most important part performance-wise
> is that you collect records with the same `a` value in a dict.
>
> For reference here's my two-pass solution to the problem as originally
> specified:
>
> bc = collections.defaultdict(lambda: ([], []))
>
> for rec in tes:
>     b, c = bc[rec["a"]]
>     b.append(rec["b"])
>     c.append(rec["c"])
>
> result = [{"a": a,
>            "b": ", ".join(b),
>            "c": ", ".join(map(str, c))}
>           for a, (b, c) in bc.items()]
>
> If you are flexible with the result data structure you could omit the
> second
> loop and use bc.items() directly.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140919/2221d88f/attachment.html>


More information about the Tutor mailing list