Last active
October 13, 2015 20:05
-
-
Save ndunn219/c32bf040f1ae4916477a to your computer and use it in GitHub Desktop.
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
# import modules | |
import re | |
def sort_streets(street_list): | |
""" | |
Sort streets alphabetically, ignoring cardinal direction prefixes such as North, South, East and West | |
:param street_list: list of street names | |
""" | |
prefix = re.compile('^North|South|East|West|N\.?|S\.?|E\.?|W\.$', re.IGNORECASE) | |
# list to store lists for sorting | |
street_sort_list = [] | |
# for every street | |
for street in street_list: | |
street_parts = street.split(' ') | |
if prefix.match(street_parts[0]) and len(street_parts) > 2: | |
street_parts.append( street_parts.pop(0) ) | |
street_sort_list.append(street_parts) | |
# sort the streets | |
street_sort_list.sort() | |
sorted_streets = [] | |
for street_parts in street_sort_list: | |
if prefix.match(street_parts[-1]) and len(street_parts) > 2: | |
street_parts.insert(0, street_parts.pop()) | |
sorted_streets.append(' '.join(street_parts)) | |
return sorted_streets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This solutions for sorting street names was inspired by Joel McCune's solution.