-2

I'm trying to check if a value contains a partial string in this sample API call result in JSON format. It should look through job title text to see if it contains the word "Virtual" or "Remote" anywhere in the string. If so, append 1 to new dictionary, and if not, append 0. I keep getting 1's for each value, but not sure why 0's don't show up if it can't find any match. Thanks! Here's the code:

import requests

remote_job_check = {'Remote':[]}

job_lists = {
      "jobs": [
        {
          "country_code": "US",
          "state": "AL",
          "title": "Sr. Customer Solutions Manager - Virtual Opportunities",
        },
        {
          "country_code": "US",
          "state": "CA",
          "title": "Data Engineer",
        },
        {
          "country_code": "US",
          "state": "CA",
          "title": "Data Engineer - Remote",
        },
        {
          "country_code": "US",
          "state": "NV",
          "title": "Remote Software Engineer",
        },
        {
          "country_code": "US",
          "state": "FL",
          "title": "Product Manager",
        }
     ]
}


job_data = requests.get(job_lists).json()['jobs']
                
for details in job_data:
    if 'Virtual' or 'Remote' in details.get('title'): 
        remote_job_check['Remote'].append(1)
    else:
        remote_job_check['Remote'].append(0)
iron502
  • 47
  • 7
  • Does this answer your question? [How to test multiple variables for equality against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value) – shriakhilc Jan 26 '22 at 22:45
  • Thanks @shriakhilc for the suggested answer, but those answers don't really cover partial text matches. One answer touched on it but stated that word boundaries would be an issue and could cause false matches. However, I did come up with a solution as seen in my answer. – iron502 Jan 27 '22 at 19:24
  • Can you please fix up the code to an [mre]? There's no point in having ``requests`` and ``json`` and such, you are effectively working with a nested list/dict/str structure. Also, many of the identifiers are illegal, e.g. ``remote-check``, ``job-data``, due to the ``-``. – MisterMiyagi Jan 27 '22 at 19:34
  • @iron502 the issue is with your understanding of how Python boolean expressions work. While that question was exact match and yours isn't, the issue is the same. You need to do `if 'Virtual' in details.get('title') or 'Remote' in details.get('title')` instead of what you have right now. You can't shorten it, because then it treats `'Virtual'` as `True` and the `or` always returns `True`, thereby always entering the if condition. – shriakhilc Jan 27 '22 at 19:49

1 Answers1

0

I was able to figure it out. Thanks for the suggestions though. I learned that my original code only searches for one of the strings. The 'or' operator wasn't even working in this case:

for details in job_data:
    if 'Virtual' or 'Remote' in details.get('title'): # The 'or' operator not working
        remote_job_check['Remote'].append(1)
    else:
        remote_job_check['Remote'].append(0)

Then I tried using just one string and it worked.

for details in job_data:
    if 'Virtual' in details.get('title'): # This works
        remote_job_check['Remote'].append(1)
    else:
        remote_job_check['Remote'].append(0)

So now I figured I need to create some sort of array to iterate through and found the 'any' function. Here's the working code:

remote_job_keywords = ['Virtual', 'Remote']
    if any(keyword in details['title'] for keyword in remote_job_keywords): 
        job_data['Remote'].append(1)
    else:
        job_data['Remote'].append(0)  

UPDATE: Thanks to @shriakhilc for the feedback on my answer. Like he said, the above works but it was easier to fix the or operator like so:

for details in job_data:
    if 'Virtual' in details.get('title') or 'Remote' in details.get('title'): 
        remote_job_check['Remote'].append(1)
    else:
        remote_job_check['Remote'].append(0)
iron502
  • 47
  • 7
  • 1
    This approach is good if you have a lot of keywords (or maybe if they can be variable). But if you have only 2, then it would be simpler to fix the `or` condition as indicated in the question comments. – shriakhilc Jan 27 '22 at 19:53
  • Thanks @shriakhilc. Your suggestion was a lot simpler. I updated my answered. – iron502 Jan 28 '22 at 01:53