Re: [Tutor] Dictionary get method

2013-03-20 Thread Mitya Sirenef
On 03/20/2013 04:21 AM, Peter Otten wrote: Phil wrote: On 20/03/13 15:09, Mitya Sirenef wrote: By the way, you can further simplify it by doing: def histogram2(s): return {c: d.get(c,0)+1 for c in s} That will work in python 3, in python 2 you need: return dict((c: d.get(c,0)

Re: [Tutor] Dictionary get method

2013-03-20 Thread Peter Otten
Phil wrote: > On 20/03/13 15:09, Mitya Sirenef wrote: > > >> >> By the way, you can further simplify it by doing: >> >> def histogram2(s): >> return {c: d.get(c,0)+1 for c in s} >> >> >> That will work in python 3, in python 2 you need: >> >> return dict((c: d.get(c,0)+1) for c in s) >

Re: [Tutor] Dictionary get method

2013-03-20 Thread Phil
On 20/03/13 15:09, Mitya Sirenef wrote: By the way, you can further simplify it by doing: def histogram2(s): return {c: d.get(c,0)+1 for c in s} That will work in python 3, in python 2 you need: return dict((c: d.get(c,0)+1) for c in s) Thanks again Mitya, although I'm not sur

Re: [Tutor] Dictionary get method

2013-03-20 Thread Phil
On 20/03/13 14:54, Amit Saha wrote: Hello Phil, On Wed, Mar 20, 2013 at 12:54 PM, Phil wrote: Thank you for reading this. I'm working my way through a series of exercises where the author only provides a few solutions. The reader is asked to modify the histogram example so that it uses the g

Re: [Tutor] Dictionary get method

2013-03-19 Thread Mitya Sirenef
On 03/20/2013 01:09 AM, Mitya Sirenef wrote: By the way, you can further simplify it by doing: def histogram2(s): return {c: d.get(c,0)+1 for c in s} That will work in python 3, in python 2 you need: return dict((c: d.get(c,0)+1) for c in s) Sorry, it should use a comma: return

Re: [Tutor] Dictionary get method

2013-03-19 Thread Mitya Sirenef
On 03/19/2013 10:54 PM, Phil wrote: Thank you for reading this. > > I'm working my way through a series of exercises where the author only provides a few solutions. > > The reader is asked to modify the histogram example so that it uses the get method thereby eliminating the if and else state

Re: [Tutor] Dictionary get method

2013-03-19 Thread Amit Saha
Hello Phil, On Wed, Mar 20, 2013 at 12:54 PM, Phil wrote: > Thank you for reading this. > > I'm working my way through a series of exercises where the author only > provides a few solutions. > > The reader is asked to modify the histogram example so that it uses the get > method thereby eliminati

Re: [Tutor] Dictionary get method

2013-03-19 Thread Mitya Sirenef
On 03/19/2013 10:54 PM, Phil wrote: Thank you for reading this. > > I'm working my way through a series of exercises where the author only provides a few solutions. > > The reader is asked to modify the histogram example so that it uses the get method thereby eliminating the if and else state

[Tutor] Dictionary get method

2013-03-19 Thread Phil
Thank you for reading this. I'm working my way through a series of exercises where the author only provides a few solutions. The reader is asked to modify the histogram example so that it uses the get method thereby eliminating the if and else statements. Histogram2 is my effort. The resul