0

Evening,

This is not necessarily pythonic, I know that going into it. However, I can not get this to trigger properly at all.

What I'm trying to do is match two letters to items in "registers" which is getting done correctly, however I'm then trying to see if there is a white space after those two letters. The white space is what's not getting picked up correctly. I'm sure that I'm just botching up the sytax. Any help would be greatly appreciated.

registers = ['R0','R1','R2','R3','R4','R5','R6','R7']
whiteSpace = ['\t', ' ']
if (item[idx +2] + item[idx +3]) in registers and (item[idx +4] in whiteSpace):
6
  • you should probably look at pyparsing ... assuming you are trying to make some kind of grammar Commented Oct 2, 2012 at 4:22
  • No errors. My test cases are just not working correctly. Also to note I can't use any string helper classes or regest for this. Commented Oct 2, 2012 at 4:32
  • Nothing is obviously wrong there. We would need to see your particular item and idx values where it fails. Commented Oct 2, 2012 at 4:36
  • idx is just an integer at this point. My test cases are "R3 " and R4 " Commented Oct 2, 2012 at 4:38
  • 1
    You realize that "R3 " and "R4 " won't work unless idx is -2, right? Commented Oct 2, 2012 at 4:39

1 Answer 1

1

It's hard to tell what you're doing wrong without seeing an example of what item is or why you're stepping through it with an index pointer. If it's just a string, as you say, you can reduce the test to the following:

if item[:2] in registers and item[-1] in whiteSpace:

You'll need to guarantee that item is 3 chars long, or put another guard in the condition.

As an aside, I like to use named slices for this sort of thing to make the intent more obvious:

code = slice(0, 2)
spacer = slice(-1)

if item[code] in registers and item[spacer] in whiteSpace:
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.