I am importing data from a .csv file in the form of 40,000 unique US zip codes, each one matched to one of 200 region codes. My goal is to produce a dictionary of each region code and its linked zip codes. I then want to pass to the dictionary several hundred thousand zip codes to get a list of the appropriate region codes. This seems like a good application of the new function Associations and key-value pairs. Specifically - I want to pass my list of values to get a list of keys. Here is where I am, using abridged and simulated data:
Import the two columns codes and skip the headers in the file
`codes = Import["filepath"][[1, 3 ;; , ;; 2]];`
This produces a list in the form of: {zip, region} such as this:
`{ {16505, 234}, {16504, 234}, {91521, 456}...}`
I then use the new GroupBy function to produce an Association of key-value pairs:
`groups = GroupBy[codes, Last -> First]`
which produces and Association in the form of:
<| 234 -> {16504, 16505},
456 -> {91521} |>
My next step was to send my list of zip codes using the Position function. Testing with a single zip does return the key value:
In= Position[groups, "16505"]
Out= {{Key[234], 2}}
Which I can Part to get the Key as such:
In= Position[groups, "16505"][[1, 1]]
Out= Key[234]
Here is where I am running into trouble. When I submit a list of zip codes I get an unexpected result back:
In= Position[groups, {"16505", "91521"}]
Out= {}
I have tried using other approaches such as the PositionIndex function and Extract. Can anyone suggest a better way to approach this?