2

I am trying to write a function that will classify goods in a given dataset (in a very straightforward way, i know).

It looks like:

def classifier(x):
    if ('smth' or 'smth' or 'smth') in x:
        return 'class1'
    elif ('smth' or 'smth' or 'smth') in x:
        return 'class2'

So, the problem is that some conditions do not work. When I try to check conditions separately - everything works. But in the function something goes wrong.

I use thing function with a pandas apply-method:

data['classes'] = data['subj'].apply(lambda x: classifier(x))
12
  • 1
    When I try to check conditions separately - everything works. But in the function something goes wrong. can you elaborate? Commented Aug 19, 2019 at 12:30
  • @TZHX of course, but it will be in Russian... is it okay for you? Commented Aug 19, 2019 at 12:31
  • both if and elif conditions are same. Commented Aug 19, 2019 at 12:31
  • 1
    @nutcracker, пайтон так не работает, ('smth' or 'smth' or 'smth') будет вычислять/сравнивать сами значения по логическому приведению, а не вхождения в список x Commented Aug 19, 2019 at 12:47
  • 1
    @glglgl, the 1st paragraph in my answer reflects the matter (and elaboration) of those comments. (I've added it after those short comments) Commented Aug 19, 2019 at 13:28

2 Answers 2

5

('smth' or 'smth' or 'smth') performs a consecutive logical comparison left-to-right, but not check for occurrence each of them within a target sequence.

To check if any value from a predefined list (iterable) occurs within a target sequence x use builtin any function:

def classifier(x):
    if any(i in x for i in ('a', 'b', 'c')):
        return 'class1'
    elif any(i in x for i in ('d', 'e', 'f')):
        return 'class2'
Sign up to request clarification or add additional context in comments.

Comments

2

You could use this:

def classifier(x):
    if 'smth' in x or 'smth' in x or 'smth' in x:
        return 'class1'
    elif 'smth' in x or 'smth' in x or 'smth' in x:
        return 'class2'

You have to check for each condition separately.

4 Comments

is there any difference?
You have to ask all conditions separately.
@nutcracker that link says it doesnt work (the example that does is a fluke and should not be relied on). ('a' or 'b') is evaulated to produce a string (or possibly boolean) first, which is then used in the membership test. You likely want to know if either is in x, so you need to ask if either is in x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.