Closed samcan closed 4 years ago
[x for x in zips if 'Milwaukie' in x['acceptable_cities']]
is an idiomatic Python solution. If you look at the code in zipcodes/init.py, it largely consists of list and dictionary comprehensions, along with similar filtering.
If I understand your issue correctly, you are proposing to add the following logic to the filter_by
function:
If one of the filter arguments specified to
filter_by
is an element ("x"), but the corresponding data attribute ("y") to be filtered is a collection type, thenfilter_by
should return all zipcodes such that y contains x.
While this feature seems potentially useful to users with a similar use-case, you were able to solve this problem with a one-line comprehension expression. I think your solution is perfectly acceptable, and others can use comprehension expressions for what they do best.
However, I may add a documentation example of your use-case, as there are likely others who have run into similar roadblocks.
Thanks for your post, @samcan.
Hi,
I'm trying to use
zipcodes.filter_by
and running into an issue. I'm trying to filter by acceptable cities to find matches. This is to catch cases where suburbs are listed under a larger metro area.An example is the zip code 97222. It could be listed as either Milwaukie, Oak Grove, or Portland in our data:
But if I try to filter by
acceptable_cities
to findMilwaukie
, I get no results:Instead, I have to filter by ALL the
acceptable_cities
:Is there a better way to do matching that I'm missing? I couldn't find an example in the documentation which covered this case.
I could do this:
Which seems to work... Is there a more efficient way of doing this?