我是一位经验丰富的Java程序员,他正在用Python重新实现一些代码,因为我只是在学习这门语言。我遇到的问题是,当我传入全局变量时,方法没有返回任何内容,但是在传入文字时返回预期的代码。代码返回传入的指定长度的单词列表,从传递的字符串开始in。例如:
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是全局变量,我看到它们被称为“危险”,虽然我不知道为什么,并且认为它们可能是这种奇怪的原因?即使用作参数的全局变量的本地副本也无济于事。也许我需要在方法的参数中使用“global”关键字,虽然从我的研究看来,除非你要更改全局变量,否则你不需要“global”关键字?任何建议或帮助将不胜感激。关于它是方法问题的可能性,这里是:
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
答案 0 :(得分: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
合并两个相邻的ifs:
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