[Python-ideas] Allow a group by operation for dict comprehension
mertz at gnosis.cx
Thu Jun 28 20:24:07 EDT 2018
I think you cheated a little in your cut-and-paste. `student_by_school` is
not defined in the code you've shown. What you **did** define, `
student_school_list` doesn't give you what you want if you use `
I thought for a moment I might just use:
[(b,a) for a,b in student_school_list]
But that's wrong for reasons that are probably obvious to everyone else.
I'm not really sure what `student_by_school` could possibly be to make this
work as shown.
On Thu, Jun 28, 2018 at 8:13 PM Chris Barker via Python-ideas <
python-ideas at python.org> wrote:
> In : student_school_list
> [('Fred', 'SchoolA'),
> ('Bob', 'SchoolB'),
> ('Mary', 'SchoolA'),
> ('Jane', 'SchoolB'),
> ('Nancy', 'SchoolC')]
> In : result = defaultdict(list, student_by_school)
> In : result.items()
> Out: dict_items([('SchoolA', ['Fred', 'Mary']), ('SchoolB', ['Bob',
> 'Jane']), ('SchoolC', ['Nancy'])])
> So: <small voice> never mind </small voice>
> Christopher Barker, Ph.D.
> Emergency Response Division
> NOAA/NOS/OR&R (206) 526-6959 voice
> 7600 Sand Point Way NE (206) 526-6329 fax
> Seattle, WA 98115 (206) 526-6317 main reception
> Chris.Barker at noaa.gov
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas