0

I am trying to control for many variables in an object. To do so I am looking to see if certain words exist in the object:

jobs = csv.DictReader(open("jobsFile.csv", "rb"))
jobsWithRoles = []

for i in jobs:
     if "Clerk" or "Stock" or "Sales" in jobs['Roles']:
          i["RoleNum"] = 1
          jobsWithRoles.append(i)
elif "Janitor" or "President" or "Driver" in jobs["Roles"]:
         i["RoleNum"] = 2
         jobsWithRoles.append(i)
else:
         i["RoleNum"] = 5
         jobsWithRoles.append(i)

The problem is that everything is getting assigned "1" regardless if they strings exist or not.

tgunn
  • 805
  • 1
  • 7
  • 10
  • 1
    Do you need to test if the current row `i` has a certain set of roles? You are testing against the `jobs` dict reader here, which doesn't support `in` membership testing anyway. – Martijn Pieters Dec 04 '13 at 19:47
  • Are `elif` and `else` well intended? Are you `else`ing the `for`? – cubuspl42 Dec 04 '13 at 20:04
  • 1
    I dont know where the response when (user deleted) but he was right. `if any(x in i['Roles'] for x in ("Clerk", "Stock", "Sales")):` – tgunn Dec 04 '13 at 20:07
  • @tgunn - I deleted my post because there was a ton of confusion as to what the OP wanted and I (at the time) didn't have enough free time to work it out. Now I wish I would have just stuck to my guns. :( –  Dec 04 '13 at 20:26
  • @iCodez, bummer man - I read your post and was implementing it - when I came back it was gone.... Big thanks regardless - I'll try to be clearer in the future – tgunn Dec 04 '13 at 20:36
  • 1
    @iCodez but you can always undelete it, and I think it's appropriate – alko Dec 04 '13 at 20:48
  • @alko - Well, I originally didn't see a need since the OP answered his own question with my answer (and gave proper citing). But if you guys want it. –  Dec 04 '13 at 20:52

3 Answers3

1

Right now, your code is being evaluated like this:

if ("Clerk") or ("Stock") or ("Sales" in jobs['Roles']):

Furthermore, since non-empty strings evaluate to True in Python, your if-statement will always pass because it is evaluating non-empty strings.

What I think you meant to do was this:

# Notice how I replaced `jobs['Roles']` with `i['Roles']`
if "Clerk" in i['Roles'] or "Stock" in i['Roles'] or "Sales" in i['Roles']:

Or, you could do this:

if any(x in i['Roles'] for x in ("Clerk", "Stock", "Sales")):

which is cleaner.

  • I am not certain that this is what the OP wants either. I suspect it should be `i['Roles'] in ("Clerk", "Stock", "Sales")` instead. Looping over all of `jobs` *per row iteration* won't work anyway. – Martijn Pieters Dec 04 '13 at 19:44
  • @MartijnPieters I don't think so. I think he is looking for job "tiers", grouping "X clerk", "Y clerk", and "Z clerk" together. And on the other hand "President", "General President" and "Vice President". But I do not know. – Hyperboreus Dec 04 '13 at 19:49
1

To begin with, I assume that your indentation is bad and you meant elif and else to correspond to if.

The first condition in your if is "Clerk" and it always evaluates to True, like all strings do. Check this out:

>>> d = dict()
>>> if "string" or "NOT THERE!" in d:
...     print("Hello world!")
... 
Hello world!

This may be a solution.

Community
  • 1
  • 1
cubuspl42
  • 6,301
  • 4
  • 33
  • 56
0

The problem is that the first condition you have always will be evaluate as True.

if "Clerk" or "Stock" or "Sales" in jobs['Roles']

So, you are saying: if "Clerk" is true or "Stock" is true, etc... .

And Python evaluates any string different than "" as true.

I recommend you use a function like:

def search_in_jobs(words, jobs):
    for w in words:
        if w in jobs: return True
    return False

then rewrite the condition:

if search_in_jobs(["Clerk", "Stock", "Sales"], jobs)
Raydel Miranda
  • 12,567
  • 2
  • 30
  • 56