这里是新手。
此代码在 python 2.7 中有效,但在 3.3 中无效
def extractFromZipFiles(zipFiles, files, toPath):
extractFunction = lambda fileName:zipFiles.extract(fileName, toPath)
map (extractFunction,files)
return
没有错误,但文件未提取。但是,当我替换为 for 循环时效果很好。
def extractFromZipFiles(zipFiles, files, toPath):
for fn in files:
zipFiles.extract(fn, toPath)
# extractFunction = lambda fileName:zipFiles.extract(fileName, toPath)
# map (extractFunction,files)
return
代码没有错误。
通常不鼓励使用map来调用函数,但话虽这么说,它不起作用的原因是Python 3返回一个生成器,而不是一个列表,因此在迭代它之前不会调用该函数。为了确保它调用函数:
list(map(extractFunction,files))
但它正在创建一个未使用的列表。更好的方法是更明确:
for file in files:
extractFunction(file)
与头部的情况一样,两条线确实比一根线更好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)