Last active
July 10, 2021 20:47
-
-
Save hongtaoh/69b607623d39341466bca33ba7fd9ab0 to your computer and use it in GitHub Desktop.
A Julia Dictionary to translate US States to Two letter codes and vice versa
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
# This Gist is based on https://gist.github.com/rogerallen/1583593 by Roger Allen | |
# | |
# United States of America Julia Dictionary to translate States, | |
# Districts & Territories to Two-Letter codes and vice versa. | |
# | |
# https://gist.github.com/hongtaoh/69b607623d39341466bca33ba7fd9ab0 | |
# | |
# Dedicated to the public domain. To the extent possible under law, | |
# Hongtao Hao has waived all copyright and related or neighboring | |
# rights to this code. | |
# If you want to preserve the order of input, replace "Dict" with "OrderedDict" | |
# To do that, run `using DataStructures` before creating the dictionary | |
# If you haven't installed DataStuctures.jl, run `import Pkg; Pkg.add("DataStructures")` | |
# | |
# The following codes were tested under Julia v1.6.1 | |
# | |
us_state_abbrev = Dict( | |
"Alabama"=> "AL", | |
"Alaska"=> "AK", | |
"American Samoa"=> "AS", | |
"Arizona"=> "AZ", | |
"Arkansas"=> "AR", | |
"California"=> "CA", | |
"Colorado"=> "CO", | |
"Connecticut"=> "CT", | |
"Delaware"=> "DE", | |
"District of Columbia"=> "DC", | |
"Florida"=> "FL", | |
"Georgia"=> "GA", | |
"Guam"=> "GU", | |
"Hawaii"=> "HI", | |
"Idaho"=> "ID", | |
"Illinois"=> "IL", | |
"Indiana"=> "IN", | |
"Iowa"=> "IA", | |
"Kansas"=> "KS", | |
"Kentucky"=> "KY", | |
"Louisiana"=> "LA", | |
"Maine"=> "ME", | |
"Maryland"=> "MD", | |
"Massachusetts"=> "MA", | |
"Michigan"=> "MI", | |
"Minnesota"=> "MN", | |
"Mississippi"=> "MS", | |
"Missouri"=> "MO", | |
"Montana"=> "MT", | |
"Nebraska"=> "NE", | |
"Nevada"=> "NV", | |
"New Hampshire"=> "NH", | |
"New Jersey"=> "NJ", | |
"New Mexico"=> "NM", | |
"New York"=> "NY", | |
"North Carolina"=> "NC", | |
"North Dakota"=> "ND", | |
"Northern Mariana Islands"=>"MP", | |
"Ohio"=> "OH", | |
"Oklahoma"=> "OK", | |
"Oregon"=> "OR", | |
"Pennsylvania"=> "PA", | |
"Puerto Rico"=> "PR", | |
"Rhode Island"=> "RI", | |
"South Carolina"=> "SC", | |
"South Dakota"=> "SD", | |
"Tennessee"=> "TN", | |
"Texas"=> "TX", | |
"Utah"=> "UT", | |
"Vermont"=> "VT", | |
"Virgin Islands"=> "VI", | |
"Virginia"=> "VA", | |
"Washington"=> "WA", | |
"West Virginia"=> "WV", | |
"Wisconsin"=> "WI", | |
"Wyoming"=> "WY" | |
) | |
us_state_abbrev_reverse = Dict() | |
for (k, v) in us_state_abbrev | |
us_state_abbrev_reverse[v] = k | |
end | |
# inspired by https://www.matthewemery.ca/post/julia-for-pythonistas/, by Matthew Emery | |
name_to_code = x -> us_state_abbrev[x] | |
code_to_name = x -> us_state_abbrev_reverse[x] | |
name_to_code("Alaska") | |
# returns "AK" | |
code_to_name("AK") | |
# returns "Alaska" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment