Re: [Tutor] Question on List of Dict

2014-09-19 Thread Sunil Tech
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 <__pete...@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@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on List of Dict

2014-09-19 Thread Peter Otten
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on List of Dict

2014-09-19 Thread Sunil Tech
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..

Thanks every one.

On Fri, Sep 19, 2014 at 1:22 PM, Danny Yoo  wrote:

>
> On Sep 19, 2014 12:28 AM, "Danny Yoo"  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?
>
> Specifically, can you write a function merge_two() such that:
>
> merge_two({''b': 'another', 'c': 754}, {'b': 'word', 'c': 745})
>
> returns the merged dictionary:
>
>{'b' : ['another', 'word'], 'c':[754, 745]}
>
> I'm trying to break the problem into simpler, testable pieces that you can
> solve.  The problem as described is large enough that I would not dare
> trying to solve it all at once.  If you have merge_two(), them you are much
> closer to a solution to the whole problem.
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on List of Dict

2014-09-19 Thread Danny Yoo
On Sep 19, 2014 12:28 AM, "Danny Yoo"  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?

Specifically, can you write a function merge_two() such that:

merge_two({''b': 'another', 'c': 754}, {'b': 'word', 'c': 745})

returns the merged dictionary:

   {'b' : ['another', 'word'], 'c':[754, 745]}

I'm trying to break the problem into simpler, testable pieces that you can
solve.  The problem as described is large enough that I would not dare
trying to solve it all at once.  If you have merge_two(), them you are much
closer to a solution to the whole problem.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on List of Dict

2014-09-19 Thread Sunil Tech
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  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?
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on List of Dict

2014-09-19 Thread Danny Yoo
>{'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?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Question on List of Dict

2014-09-18 Thread Sunil Tech
Hi all,

tes = [{'a': 1, 'b': 'this', 'c': 221},
   {'a': 2, 'b': 'this', 'c': 215},
   {'a': 1, 'b': 'is', 'c': 875},
   {'a': 1, 'b': 'sentence', 'c': 874},
   {'a': 2, 'b': 'another', 'c': 754},
   {'a': 2, 'b': 'word', 'c': 745}]

The above one is  the result form the DB. I am trying to convert it to
something like

result_tes = [{'a': 1, 'b': 'this, is, sentence', 'c': '221, 875, 874'},
  {'a': 2, 'b': 'this, another, word', 'c': '215, 754, 744'}]

if the value of the 'a' is same, then all those other values of the dict
should be merged/clubbed.

I tried, but it became complex and complex.

please can any one help me to get the result.


Thanks,
Sunil. G
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor