【发布时间】:2013-01-30 16:21:02
【问题描述】:
我只是想修改一个字符串并返回修改后的字符串,但是,在打印变量时我得到“无”返回。
def AddToListTwo(self,IndexPosition):
filename = RemoveLeadingNums(self, str(self.listbox1.get(IndexPosition))) #get the filename, remove the leading numbers if there are any
print filename #this prints None
List2Contents = self.listbox2.get(0, END)
if(filename not in List2Contents): #make sure the file isn't already in list 2
self.listbox2.insert(0, filename)
def RemoveLeadingNums(self, words):
if(isinstance(words,str)):
match = re.search(r'^[0-9]*[.]',words)
if match: #if there is a match, remove it, send it through to make sure there aren't repeating numbers
RemoveLeadingNums(self, re.sub(r'^[0-9]*[.]',"",str(words)).lstrip())
else:
print words #this prints the value correctly
return words
if(isinstance(words,list)):
print "list"
编辑 - 多人评论说如果有匹配项我不会返回值。如果有的话我不想退货。它可能会重复(例如:1.2. itema)。所以,我想基本上使用递归来删除它,然后返回值
【问题讨论】:
-
这段代码看起来很多余。为什么
RemoveLeadingNums以递归开头? -
递归。该值可能是“1.2.4. item”,我想在 item 之前删除整组值。
-
为了响应您的编辑,如果您不想在匹配时返回任何内容,请不要将结果分配给
filename。这段代码没有意义。 -
查看您的编辑 -- 我不确定您是否理解
re.sub所做的 -- 或者您可能不了解字符串的不变性?我不确定,但在我看来你在这里遗漏了一些东西...... -
我已经解释过你使用递归错误。我知道你认为你正在尝试做什么,但它不会起作用(更努力地思考递归将如何发挥作用)。阅读:mywiki.wooledge.org/XyProblem