0

Depending on which classroom a pupil is in, I would like to return one of two values and assign it to the variable name 'scoreMutiplier' which would then be used later elsewhere in my code. However, I am still pretty new to this and have been confronted with the following error message after the first scenario when I execute my code: AttributeError: 'str' object has no attribute 'isin'

import numpy as np

pupil = 'Tom'

classroom_A = ['Peter', 'Greg', 'Susan', 'Tom', 'John']

classroom_B = ['Steve', 'Joe', 'Jose', 'Pam', 'Paul']

scenario = [((pupil.isin(classroom_A )) & (~pupil.isin(classroom_B ))),
            ((pupil.isin(classroom_B )) & (~pupil.isin(classroom_A )))]

result = [3,1]

scoreMultiplier = np.select(scenario , result, default='')

print(scoreMultiplier)

What is the best way to make this work?

Many thanks in advance.

2 Answers 2

1

You got the right idea, but are using the wrong function. A str object does not have an isin() function. You should use the keyword in which, in the case of a list, check if the list contains the given object (in other cases it calls the object's __contains__() function). You should also use Python's binary operators (and, or, not) when working with boolean instead of bitwise operators. So that would mean to change this part of the code to the following:

scenario = [pupil in classroom_A and pupil not in classroom_B,
            pupil in classroom_B and pupil not in classroom_A]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks thats seems to have done it. However, I got a warning message:- DeprecationWarning: select condlists containing integer ndarrays is deprecated and will be removed in the future. Use .astype(bool) to convert to bools. This actually prevented the use of the variable in a calculation further down in my code, which I got around by converting the variable to a float. But how should I change my code to comply with what the warning message is suggesting?
It's hard to answer this only through comments, I don't know which variable is this concerning. When I run the above code there is no warning message.
"....You should also use Python's binary operators (and, or, not) when working with boolean instead of bitwise operators...' your explanation and the amendment @Samansi Hymavathi address the final problem that I was having perfectly. Ace!!!!!!
0

Check this code:

import numpy as np

pupil = 'Tom'

classroom_A = ['Peter', 'Greg', 'Susan', 'Tom', 'John']

classroom_B = ['Steve', 'Joe', 'Jose', 'Pam', 'Paul']

scenario = [pupil in classroom_A and pupil not in classroom_B,
            pupil in classroom_B and pupil not in classroom_A]

result = [3,1]

scoreMultiplier = np.select(scenario , result, default='')

print(scoreMultiplier)

Comments

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.