我有一个任务,我被要求修改一些代码。原有的功能是这样的:在功能中使用lambdas函数
def selectivelyCopy(inputFile,outputFile,predicate):
linesCopied = 0
for line in inputFile:
if predicate(line):#test the line with the predicate
outputFile.write(line)
linesCopied+=1
inputFile.close()
return linesCopied
现在我想添加参数变换,即根据用户指定的转变发生在作为其参数字符串,并返回一个字符串的函数。如果变换从函数调用中省略,则输入文件中的行将被写入为未更改。
这是我到目前为止有:
def selectivelyCopy2(inputFile,outputFile,predicate, transform):
def transform(x = lambda x: x):
return(x)
linesCopied = 0
for line in inputFile:
if predicate(line): #test the line with the predicate
outputFile.write(line)
linesCopied+=1
inputFile.close()
return linesCopied
我不知道从哪里这里着手。我想我想读取输入文件行,但写出转换后的行......或其他东西?
尝试运行代码。你的CPU时间比我们的价值低。如果这是家庭作业,也标记为家庭作业。 – Shep 2012-04-16 06:25:13
我确实运行过它。它就好像定义不在那里一样。没有错误。 – 2012-04-16 06:38:44
对不起,明显的,但是......你运行上面的代码?没有额外的东西?你有没有呼唤你的功能? – Veky 2016-08-07 16:39:10