Created
April 20, 2012 04:31
-
-
Save violetcode/2426014 to your computer and use it in GitHub Desktop.
Alphabetizer Helper Thingy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def alphabetizer(names): | |
""" | |
Takes a list as a parameter, and sorts them into alphabetical order | |
and groups them by the first letter. | |
""" | |
result = [] | |
temp_dict = {} | |
for name in names: | |
first_letter = name[0].upper() | |
if first_letter not in temp_dict: | |
temp_dict[first_letter] = [] | |
temp_dict[first_letter].append(name) | |
for item, val in temp_dict.iteritems(): | |
val.sort() | |
result.append({'letter': item, 'list': val}) | |
return result | |
""" | |
SAMPLE TEMPLATE | |
some_dict is the data structure returned by the alphabetizer method | |
<ul> | |
{% for item in some_dict|dictsort:"letter" %} | |
<li>{{item.letter}}</li> | |
<ul> | |
{% for val in item.list %} | |
<li>{{val}}</li> | |
{% endfor %} | |
</ul> | |
{% endfor %} | |
</ul> | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment