简体   繁体   中英

Python Syntax White Spaces

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):

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM