1

I am new to Python and I am trying to filter out a string based on some criteria. I just want throw an error when there is not a 'c' after the second '/' or the string does not start with 'a'. Here is what I am doing -

if sample_data.split('/')[2] != 'c' or sample_data[:1] != 'a':
    print('Unexpected data')
    exit()

print('Further processing')

here is output based on the input I pass -

sample_data = 'a/b/c/d output - Further processing --> AS EXPECTED
sample_data = 'x/b/y/d output - Unexpected data --> AS EXPECTED
sample_data = 'a/b/y/d output - Unexpected data --> NOT AS EXPECTED
sample_data = 'x/b/c/d output - Unexpected data --> NOT AS EXPECTED

I guess I am missing something basic here.

8
  • I think you are using 'or' wrong here (in your natural language description). You want to throw an error when the string does not start with 'a' and when it does not have 'c' after the second '/'. Commented Jun 7, 2021 at 17:02
  • Hey Dan - your if statement is actually executing correctly, per your description. The first failed example does not have c after the second /. The second failed example does not start with a. Is this not your criteria? Like @UziGoozie said, you may be looking for and instead of or. Commented Jun 7, 2021 at 17:05
  • @UziGoozie - When the string starts with 'a' it does not matter if there is 'c' or not. It should be valid. Same with if there is a 'c' in the position then does not matter if there is an 'a' in the beginning. Commented Jun 7, 2021 at 17:10
  • @gmdev - yes in the first failed example it does not have a 'c' in that position but since it starts with 'a' it should be valid. Same with the second failed scenario. Maybe i am using the if statement wrong Commented Jun 7, 2021 at 17:14
  • ok. this is weird. Using and works. But "and" evaluates both the conditions and if both are true then it returns true. If the first check is false it does not check the second condition since it will always return false. is my understanding wrong here? Commented Jun 7, 2021 at 17:21

1 Answer 1

4

In natural language, you'd say:

The data is valid if it starts with an a OR it contains a c in the third location. When you invert this, you need to use de Morgan's rule:

not(A or B) = not(A) and not(B)

In words: The data is invalid if it doesn't start with a AND it doesn't contain c in the third location.

To verify this, let's make a truth table:

sample_data.split('/')[2] != 'c' (Doesn't contain c) sample_data[:1] != 'a' (Doesn't start with a) Result (throw error?)
c (False) a (False) Valid (False)
c (False) * (True) Valid (False)
* (True) a (False) Valid (False)
* (True) * (True) Invalid (True)

The only case where you want an error is when sample_data.split('/')[2] != 'c' and sample_data[:1] != 'a'. That's what your if statement needs to have.

samples = [
    'a/b/c/d',
    'x/b/c/d',
    'a/b/y/d', 
    'x/b/y/d',
]

for sample_data in samples:
    print(f"Input: {sample_data}", end=" ")
    if sample_data.split('/')[2] != 'c' and sample_data[:1] != 'a':
        print('Output: Unexpected data')
    else:
        print('Output: Further processing')

gives what you expect:

Input: a/b/c/d Output: Further processing
Input: x/b/c/d Output: Further processing
Input: a/b/y/d Output: Further processing
Input: x/b/y/d Output: Unexpected data
Sign up to request clarification or add additional context in comments.

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.