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))
Nobody
  • 53
  • 11
nutcracker
  • 101
  • 1
  • 9
  • 1
    `When I try to check conditions separately - everything works. But in the function something goes wrong.` can you elaborate? – TZHX Aug 19 '19 at 12:30
  • @TZHX of course, but it will be in Russian... is it okay for you? – nutcracker Aug 19 '19 at 12:31
  • both `if` and `elif` conditions are same. – Harsha Biyani Aug 19 '19 at 12:31
  • @nutcracker nyet, sadly. – TZHX Aug 19 '19 at 12:31
  • @HarshaBiyani it is an example, just to show the structure – nutcracker Aug 19 '19 at 12:32
  • does [this question/answer pair](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) help you? – TZHX Aug 19 '19 at 12:33
  • @nutcracker используй более реалистичные примеры, чем дубликаты в виде `'smth' or 'smth' or 'smth'` – RomanPerekhrest Aug 19 '19 at 12:35
  • @RomanPerekhrest ну, к примеру, у меня есть позиция 'Сейф №(что-то там)' и внутри условия if если у меня `if 'Сейф' in x:`, то условие не срабатывает, а если проверять отдельно, то сработает – nutcracker Aug 19 '19 at 12:36
  • 1
    @nutcracker, пайтон так не работает, `('smth' or 'smth' or 'smth')` будет вычислять/сравнивать сами значения по логическому приведению, а не вхождения в список `x` – RomanPerekhrest Aug 19 '19 at 12:47
  • @nutcracker, welcome, лови лайк ) – RomanPerekhrest Aug 19 '19 at 12:50
  • @RomanPerekhrest and OP, questions and answers, as well as their respective comments, are supposed to be useable for everyone here. So yould anybody check if there is valuable information and translate it to English? – glglgl Aug 19 '19 at 13:25
  • 1
    @glglgl, the 1st paragraph in my answer reflects the matter (and elaboration) of those comments. (I've added it after those short comments) – RomanPerekhrest Aug 19 '19 at 13:28

2 Answers2

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'
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
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.

Sid
  • 2,174
  • 1
  • 13
  • 29
  • is there any difference? – nutcracker Aug 19 '19 at 12:36
  • You have to ask all conditions separately. – Sid Aug 19 '19 at 12:38
  • why? https://python-forum.io/Thread-Simplifying-multiple-or-conditions-in-if-statement – nutcracker Aug 19 '19 at 12:40
  • 2
    @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`. – D. Ben Knoble Aug 19 '19 at 12:44