Python 中的字符串连接与字符串替换 [英] String concatenation vs. string substitution in Python

查看:62
本文介绍了Python 中的字符串连接与字符串替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,我不知道何时何地使用字符串连接与字符串替换.由于字符串连接在性能上有很大的提升,这是(变得更多)一个风格决定而不是一个实际的决定吗?

举一个具体的例子,应该如何处理灵活 URI 的构造:

DOMAIN = 'http://stackoverflow.com'问题 = '/问题'def so_question_uri_sub(q_num):返回%s%s/%d"%(域、问题、q_num)def so_question_uri_cat(q_num):返回域 + 问题 + '/' + str(q_num)

还有关于加入字符串列表和使用命名替换的建议.这些是中心主题的变体,也就是说,在什么时间做哪种方式是正确的?感谢您的回复!

解决方案

根据我的机器,连接速度(显着)更快.但在风格上,如果性能不重要,我愿意为替代付出代价.好吧,如果我需要格式化,甚至不需要问这个问题……除了使用插值/模板之外别无选择.

<预><代码>>>>导入时间>>>def so_q_sub(n):...返回%s%s/%d"%(域,问题,n)...>>>so_q_sub(1000)'http://stackoverflow.com/questions/1000'>>>def so_q_cat(n):...返回域 + 问题 + '/' + str(n)...>>>so_q_cat(1000)'http://stackoverflow.com/questions/1000'>>>t1 = timeit.Timer('so_q_sub(1000)','from __main__ import so_q_sub')>>>t2 = timeit.Timer('so_q_cat(1000)','from __main__ import so_q_cat')>>>t1.timeit(数字=10000000)12.166618871951641>>>t2.timeit(数字=10000000)5.7813972166853773>>>t1.timeit(数字=1)1.103492206766532e-05>>>t2.timeit(数字=1)8.5206360154188587e-06>>>def so_q_tmp(n):... return "{d}{q}/{n}".format(d=DOMAIN,q=QUESTIONS,n=n)...>>>so_q_tmp(1000)'http://stackoverflow.com/questions/1000'>>>t3= timeit.Timer('so_q_tmp(1000)','from __main__ import so_q_tmp')>>>t3.timeit(数字=10000000)14.564135316080637>>>def so_q_join(n):... return ''.join([DOMAIN,QUESTIONS,'/',str(n)])...>>>so_q_join(1000)'http://stackoverflow.com/questions/1000'>>>t4= timeit.Timer('so_q_join(1000)','from __main__ import so_q_join')>>>t4.timeit(数字=10000000)9.4431309007150048

In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?

For a concrete example, how should one handle construction of flexible URIs:

DOMAIN = 'http://stackoverflow.com'
QUESTIONS = '/questions'

def so_question_uri_sub(q_num):
    return "%s%s/%d" % (DOMAIN, QUESTIONS, q_num)

def so_question_uri_cat(q_num):
    return DOMAIN + QUESTIONS + '/' + str(q_num)

Edit: There have also been suggestions about joining a list of strings and for using named substitution. These are variants on the central theme, which is, which way is the Right Way to do it at which time? Thanks for the responses!

解决方案

Concatenation is (significantly) faster according to my machine. But stylistically, I'm willing to pay the price of substitution if performance is not critical. Well, and if I need formatting, there's no need to even ask the question... there's no option but to use interpolation/templating.

>>> import timeit
>>> def so_q_sub(n):
...  return "%s%s/%d" % (DOMAIN, QUESTIONS, n)
...
>>> so_q_sub(1000)
'http://stackoverflow.com/questions/1000'
>>> def so_q_cat(n):
...  return DOMAIN + QUESTIONS + '/' + str(n)
...
>>> so_q_cat(1000)
'http://stackoverflow.com/questions/1000'
>>> t1 = timeit.Timer('so_q_sub(1000)','from __main__ import so_q_sub')
>>> t2 = timeit.Timer('so_q_cat(1000)','from __main__ import so_q_cat')
>>> t1.timeit(number=10000000)
12.166618871951641
>>> t2.timeit(number=10000000)
5.7813972166853773
>>> t1.timeit(number=1)
1.103492206766532e-05
>>> t2.timeit(number=1)
8.5206360154188587e-06

>>> def so_q_tmp(n):
...  return "{d}{q}/{n}".format(d=DOMAIN,q=QUESTIONS,n=n)
...
>>> so_q_tmp(1000)
'http://stackoverflow.com/questions/1000'
>>> t3= timeit.Timer('so_q_tmp(1000)','from __main__ import so_q_tmp')
>>> t3.timeit(number=10000000)
14.564135316080637

>>> def so_q_join(n):
...  return ''.join([DOMAIN,QUESTIONS,'/',str(n)])
...
>>> so_q_join(1000)
'http://stackoverflow.com/questions/1000'
>>> t4= timeit.Timer('so_q_join(1000)','from __main__ import so_q_join')
>>> t4.timeit(number=10000000)
9.4431309007150048

这篇关于Python 中的字符串连接与字符串替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆