我很难使用Python的timeit.Timer(stmt,setup_stmt)中的setup语句。我很感激你能帮我解决这个棘手的问题:
所以我的狙击手是这样的:
def compare(string1, string2):
# compare 2 strings
if __name__ = '__main__':
str1 = "This string has \n several new lines \n in the middle"
str2 = "This string hasn't any new line, but a single quote ('), in the middle"
t = timeit.Timer('compare(p1, p2)', "from __main__ import compare; p1=%s, p2=%s" % (str1,str2))
我不知道如何在不更改setup语句中的含义的情况下转义变量str1、str2中的元字符:
"from __main__ import compare; p1=%s, p2=%s" % (str1,str2)
我尝试了各种组合,但总是有以下错误:
SyntaxError:无法赋值给literal
SyntaxError:扫描单引号字符串时下线
语法错误:无效语法
目前没有回答
相关问题 更多 >
编程相关推荐