0

我是一位经验丰富的 Java 程序员,正在用 Python 重新实现一些代码,因为我只是在学习这门语言。我遇到的问题是,当我传入全局变量时,一个方法没有返回任何内容,但在传入文字时返​​回预期的代码。代码返回传入的指定长度的单词列表,从传递的字符串开始例如:

print getNGramBeginsWords("ha", 5)

返回

['HAAFS', 'HAARS', 'HABIT', 'HABUS', 'HACEK', 'HACKS', 'HADAL', 'HADED', 'HADES',
 'HADJI', 'HADST', 'HAEMS', 'HAETS', 'HAFIZ', 'HAFTS', 'HAHAS', 'HAIKA', 'HAIKS',
 'HAIKU', 'HAILS', 'HAINT', 'HAIRS', 'HAIRY', 'HAJES', 'HAJIS', 'HAJJI', 'HAKES', 
 'HAKIM', 'HAKUS', 'HALAL', 'HALED', 'HALER', 'HALES', 'HALID', 'HALLO', 'HALLS',
 'HALMA','HALMS', 'HALON', 'HALOS', 'HALTS', 'HALVA', 'HALVE', 'HAMAL', 'HAMES',
 'HAMMY', 'HAMZA', 'HANCE', 'HANDS', 'HANDY', 'HANGS', 'HANKS', 'HANKY', 'HANSA', 
 'HANSE', 'HANTS', 'HAOLE', 'HAPAX', 'HAPLY', 'HAPPY', 'HARDS', 'HARDY', 'HARED',
 'HAREM', 'HARES', 'HARKS', 'HARLS', 'HARMS', 'HARPS', 'HARPY', 'HARRY', 'HARSH', 
 'HARTS', 'HASPS', 'HASTE', 'HASTY', 'HATCH', 'HATED', 'HATER', 'HATES', 'HAUGH', 
 'HAULM', 'HAULS', 'HAUNT', 'HAUTE', 'HAVEN', 'HAVER', 'HAVES', 'HAVOC', 'HAWED', 
 'HAWKS', 'HAWSE', 'HAYED', 'HAYER', 'HAYEY', 'HAZAN', 'HAZED', 'HAZEL', 'HAZER', 
 'HAZES']

正如它应该。然而,

print inputString
print numLetters
print getNGramBeginsWords(inputString, numLetters)

返回

ha
5
[]

inputString 和 numLetters 是全局变量,我见过它们被称为“危险”,尽管我不知道为什么,并且认为它们可能是造成这种奇怪现象的原因?即使是用作参数的全局变量的本地副本也无济于事。也许我需要在方法的参数中使用“全局”关键字,尽管从我的研究看来,除非您更改全局变量,否则您不需要“全局”关键字?任何建议或帮助将不胜感激。如果这是方法的问题,这里是:

def getNGramBeginsWords(nGram, length):
    dict = open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r')
    nGram = nGram.upper()
    words = []
    for line in dict:
        if(len(line)>0):
            if(len(nGram)>len(line.strip()) | len(line.strip())!= length):
                continue
            s = line.strip()[:len(nGram)]
            if(s == nGram and len(line.strip()) == length):
                words.append(line.strip())
    return words
4

1 回答 1

1

tl; dr:全局变量与此无关;几乎可以肯定,您传递的是字符串而不是 int 作为长度参数。你的代码有很多冗余。

您的代码有许多明显的问题,包括风格和实质性问题:

def getNGramBeginsWords(nGram, length):
    # dict is the name of a builtin function, which you are confusingly overwriting
    # dict = open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r')
    wlist = open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r')
    nGram = nGram.upper()
    words = []
    for line in wlist:
        # an empty string evaluates to False in a binary context; also no need for those brackets
        stripline = line.strip().upper() # you keep doing this; I added the upper here.
        # you don't need this if, because you immediately test length
        #if stripline: #I know I changed this, but you only refer to the stripped version below 
        # pipe | is bitwise OR. I bet you don't want that
        if len(nGram)>len(stripline) or len(stripline)!= length:
            continue
        # s = stripline[:len(nGram)] #you only use this once
        # you don't need to check that stripline is of length again; you already did that
        # also, you can just use `endswith` instead of slicing
        if stripline.endswith(nGram):
            words.append(stripline)
    return words

而且,没有评论:

def getNGramBeginsWords(nGram, length):
    wlist = open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r')
    nGram = nGram.upper()
    words = []
    for line in wlist:
        stripline = line.strip() # you keep doing this
        # you can merge these two ifs
        if len(nGram)>len(stripline) or len(stripline)!= length:
            continue
        if stripline.endswith(nGram):
            words.append(stripline)
    return words

合并两个相邻的 if:

def getNGramBeginsWords(nGram, length):
    wlist = open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r')
    nGram = nGram.upper()
    words = []
    for line in wlist:
        stripline = line.strip().upper() # you keep doing this
        # you can merge these two ifs
        # also this renders the comparison of ngram and stripline lengths redundant
        if (len(stripline) == length) and stripline.endswith(nGram):
            words.append(stripline)
    return words

现在,让我们看看最后一个版本 - 有趣的是,您实际上从未对length. 鉴于长度应该是一个数字,您可能希望将其强制为一个数字;如果它不能被转换,你会得到一个例外。

def getNGramBeginsWords(nGram, length):
    wlist = open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r')
    nGram = nGram.upper()
    words = []
    length = int(length) # force to an int
    assert isinstance(n, int) # or do this if you prefer to get an exception on all invalid input
    for line in wlist:
        stripline = line.strip().upper() # you keep doing this
        # you can merge these two ifs
        if (len(stripline) == length) and stripline.endswith(nGram):
            words.append(stripline)
    return words

最后,您永远不会真正明确地关闭文件。你会让它闲逛一段时间。最好使用with构造自动关闭它:

def getNGramBeginsWords(nGram, length):
    with open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r') as wlist:
        nGram = nGram.upper()
        words = []
        length = int(length) # force to an int
        assert isinstance(n, int) # or do this if you prefer to get an exception on all invalid input
        for line in wlist:
            stripline = line.strip().upper() # you keep doing this
            #you should be using `endswith` instead of the slice
            if (len(stripline) == length) and stripline.endswith(nGram):
                words.append(stripline)
        return words
于 2013-07-17T20:54:43.060 回答