<div dir="ltr">I have use exercises with dictionaries which required the get() method ...especially the feature which when the particular key was not found, a dictionary element was created with a specified value. <div><br></div><div>""" Returning home next meet-up. """</div><div>pete</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 25, 2015 at 8:55 PM, Eric Floehr <span dir="ltr"><<a href="mailto:eric@intellovations.com" target="_blank">eric@intellovations.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>I had a dictionary indexed by strings, with the value being an array of one or more items. I wanted to know how many had 1 item, how many had 2, etc.<br><br></div><div>My solution was to use Counter from collections:<br><br></div>>>> from collections import Counter<br>>>> d = { 'A': [1], 'B': [2,3], 'C': [4], 'D': [1,2,3]}<br>>>> lens = [ len(value) for value in d.values() ]<br>>>> print(lens)<br>[1, 1, 2, 3]<br>>>> counts = Counter(lens)<br>>>> print(counts)<br>Counter({1: 2, 2: 1, 3: 1})<br>>>> counts[1]<br>2<br><br></div><div>In my case it was a dictionary of 850 entries, with arrays of varying size. It made understanding the composition of the the dictionary easier.<br><br></div><div>What Python module/class/function has made your life easier?<br><br></div></div></div>
<br>_______________________________________________<br>
CentralOH mailing list<br>
<a href="mailto:CentralOH@python.org">CentralOH@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/centraloh" target="_blank">https://mail.python.org/mailman/listinfo/centraloh</a><br>
<br></blockquote></div><br></div>