简体   繁体   English

Python语法空白

[英]Python Syntax White Spaces

Evening, 晚间,

This is not necessarily pythonic, I know that going into it. 这不一定是pythonic,我知道要深入了解它。 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. 如果不查看示例item是什么或为什么要使用索引指针单步执行该示例,就很难说出自己在做什么错。 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. 您需要确保该商品的长度为3个字符,或在状况中放置另一个警卫。

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:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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