在for循环中使用嵌套的if语句

时间:2016-10-02 15:59:53

标签: python string iteration

我试图通过一个字符串(没有空格)读取实例,其中有两个单独的小写字母被3个大写字母包围(即HHSkSIO)。我已经编写了以下代码:

def window(fseq, window_size=7):
    for i in xrange(len(fseq) - window_size + 1):
        yield fseq[i:i+window_size]


for seq in window('asjdjdfkjdhfkdjhsdfkjsdHJJnJSSsdjkdsad', 7):
    if seq[0].isupper and seq[1].isupper and seq[2].isupper and seq[3].islower and seq[4].isupper and seq[5].isupper and seq[6].isupper:
            print seq

其中第一个函数窗口允许我使用7的滑动窗口迭代字符串,而第二个部分,for语句,检查每个窗口内的字符是否越高越高越低越高越高。当我运行代码时,它出现了:

asjdjdf
sjdjdfk
jdjdfkj
djdfkjd
jdfkjdh
dfkjdhf
fkjdhfk
kjdhfkd
jdhfkdj
dhfkdjh
hfkdjhs
fkdjhsd
kdjhsdf
djhsdfk
jhsdfkj
hsdfkjs
sdfkjsd
dfkjsdH
fkjsdHJ
kjsdHJJ
jsdHJJn
sdHJJnJ
dHJJnJs
HJJnJsd
JJnJsdj
JnJsdjk
nJsdjkd
Jsdjkds
sdjkdsa
djkdsad

如何使for语句打印出符合上述if语句的滑动窗口,而不是打印出所有这些? P.S我知道这可能是一种非常笨重的方式,我是一个初学者,这是我唯一能想到的东西!

2 个答案:

答案 0 :(得分:1)

您必须调用isupperislower方法:

    if seq[:3].isupper() and seq[3].islower() and seq[4:].isupper():
        print seq

答案 1 :(得分:1)

问题是你在调用.isupper时缺少(),它总是评估为true。

尝试:

def window(fseq, window_size=7):
    for i in range(len(fseq) - window_size + 1):
        yield fseq[i:i+window_size]


for seq in window('asjdjdfkjdhfkdjhsdfkjsdHJJnJSSsdjkdsad', 7):
    if seq[0].isupper() and seq[1].isupper() and seq[2].isupper() and seq[3].islower() and seq[4].isupper() and seq[5].isupper() and seq[6].isupper():
        print (seq)

另一种方法是:

import re
s = re.compile(r'[A-Z]{3}[a-z][A-Z]{3}')
def window(fseq, window_size=7):
    for i in range(len(fseq) - window_size + 1):
        yield fseq[i:i+window_size]

for seq in window('asjdjdfkjdhfkdjhsdfkjsdHJJnJSSsdjkdsad', 7):
    result = s.search(seq)
    if result is not None:
        print(result.group())