简体   繁体   English

Python 挑战级别 3。为什么我的代码不起作用?

[英]Python Challenge level 3. Why isn't my code working?

So I know this is very dirty and probably inefficient code, but I was just trying to see if I could get it to work.所以我知道这是非常脏而且可能效率低下的代码,但我只是想看看我是否可以让它工作。 I just don't understand why it's not working... The text from the file I'm accessing is in the source of http://www.pythonchallenge.com/pc/def/equality.html .我只是不明白为什么它不起作用......我正在访问的文件中的文本位于http://www.pythonchallenge.com/pc/def/equality.html的源代码中。 Any help?有什么帮助吗?

#! /usr/bin/python

# This program searches a string for a lowercase letter that has 3, and only 3,
# uppercase letters to the left of it, and 3, and only 3, uppercase letters to
# the right of it. It then returns that lowercase letter.

from string import lowercase
from string import uppercase

# Open the text file containing the string and set it to variable str
fileHandle = open ( 'bodyguards.txt', 'r' )
str = fileHandle.read()
fileHandle.close()
#str = 'BIGaBIGaBIGaBIG'

# Search for a lowercase letter.
def isitlower(str):
    for a in str :
        if a in lowercase:
            letterposition = str.index(a) 
            r =threebgright(letterposition)
            l =threebgleft(letterposition)
            if r !=None and l !=None:
                print l,":",a,":", r

def threebgright(letterposition):
    if str[letterposition + 1] in uppercase and str[letterposition +2] in uppercase and         str[letterposition + 3] in uppercase and str[letterposition + 4] not in uppercase:
        return str[letterposition+1], str[letterposition+2], str[letterposition+3]
    else:
        return None

def threebgleft(letterposition):
    if str[letterposition - 1] in uppercase and str[letterposition -2] in uppercase and     str[letterposition - 3] in uppercase and str[letterposition - 4] not in uppercase:
        return str[letterposition-1], str[letterposition-2], str[letterposition-3]
    else:
        return None

isitlower(str)

This will find the index of the first occurrence of a in str:这将找到 a 在 str 中第一次出现的索引:

letterposition = str.index(a)

Probably not what you want.可能不是你想要的。 The correct way would be to use enumerate :正确的方法是使用enumerate

def isitlower(my_str):
    for a, letter_position in enumerate(my_str):
        if a in lowercase:
            r =threebgright(letterposition)
            l =threebgleft(letterposition)
            if r !=None and l !=None:
                print l,":",a,":", r

It's a good idea not to use str as a variable name since it's a builtin type / function最好不要使用str作为变量名,因为它是内置类型/函数

One of the many benefits of python is regular expressions. python 的众多好处之一是正则表达式。 Regex in python can be accessed by importing the re module.Your answer is too complex , tiring and inefficient.It's best to follow KISS (Keep it simple and small). python中的正则表达式可以通过导入re模块来访问。你的回答太复杂,累人,效率低下。最好跟着KISS(Keep it simple and small)。 An easier solution would be :一个更简单的解决方案是:

data = " All that huge text "

bodyguarded = re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]')
print "".join(bodyguared)

Explaination :说明

[^A-Z]

Bear in mind that there should be exactly 3 big letters sourounding the small one.请记住,小字母周围应该正好有3 个大字母。 If we removed this, things like 'SJDHjHDG' would come to be correct.如果我们删除它,“SJDHjHDG”之类的东西就会变得正确。 What this is basically doing is that it doesn't want any caps before the occurence of the three caps which is defined by [AZ]{3}.这基本上做的是在由 [AZ]{3} 定义的三个大写字母出现之前它不想要任何大写字母。

Next is ([az]) .接下来是([az]) This is how you define the single lowercase letter in the middle.这就是定义中间单个小写字母的方式。 Notice how it doesn't have an {x} next to it.This is because we only want one occurence.注意它旁边没有 {x}。这是因为我们只希望出现一次 For example : 'ABCjjKLM' is not correct since the lowercase chars in the middle are more than one.例如: 'ABCjjKLM'不正确,因为中间的小写字符不止一个。

We want the lowercase letter to be sourrounded and therefore we want another 3 occurences on the right side ( [AZ]{3} ), but no more , and that's why we add [^AZ] .我们希望小写字母被环绕,因此我们希望在右侧再出现 3 次( [AZ]{3} ),但不再出现,这就是我们添加[^AZ] As i explained earlier cases like 'AJDkKLKM' or 'JHKHjLKKK' are not correct because we want ONLY 3 occurences of capital letters.正如我之前解释过的,像'AJDkKLKM''JHKHjLKKK'这样'AJDkKLKM'是不正确的,因为我们只需要 3 次大写字母。 This is saying that after the 3 caps ( [AZ]{3} ) any character except those in the range of A to Z are valid.也就是说,在 3 个大写字母 ( [AZ]{3} ) 之后,除了 A 到 Z 范围内的字符之外的任何字符都是有效的。

Last of all there is print "".join(bodyguard)最后是print "".join(bodyguard)

I did this just for the sake of readability.我这样做只是为了可读性。

The findall function of the re module returns its findings in a list. re 模块的 findall 函数在列表中返回其结果。 if we did print bodyguard we would get something like this : [x,y,z,r,q,i] Instead we get 'xyzrqi' because all the elements of the list are joined together in a string by "" which is empty.如果我们确实print bodyguard我们会得到这样的结果: [x,y,z,r,q,i]相反,我们得到的是'xyzrqi'因为列表中的所有元素都通过""连接到一个字符串中,该字符串为空.

Hope I helped.希望我有所帮助。

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

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