替换字符串中第一个单词的最佳方法? [英] best way to replace first word in string?

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

问题描述

我正在寻找最好和最有效的方法来替换str中的第一个单词

,如下所示:

aa to be become - > / aa /成为

我知道我可以使用溢出而不是加入他们

但我也可以使用正则表达式

和我确定有很多方法,但我需要真正有效的方法

I am looking for the best and efficient way to replace the first word
in a str, like this:
"aa to become" -> "/aa/ to become"
I know I can use spilt and than join them
but I can also use regular expressions
and I sure there is a lot ways, but I need realy efficient one

推荐答案

有一个陷阱。对此:


如何定义word? (例如,

第一个单词后面可以跟空格,逗号,句号,
或其他标点符号,还是总是空格)。


如果它总是一个空格那么这将是相当的

高效。


string =" aa to become

firstword,restwords = s.split('''',1)

newstring =" /%s /%s" %(firstword,restwords)


我敢肯定这里的正则表达大师可以来一些东西,如果它可以跟着< br $>
a空间。


-Larry Bates
ha ** ***@gmail.com 写道:
There is a "gotcha" on this:

How do you define "word"? (e.g. can the
first word be followed by space, comma, period,
or other punctuation or is it always a space).

If it is always a space then this will be pretty
"efficient".

string="aa to become"
firstword, restwords=s.split('' '',1)
newstring="/%s/ %s" % (firstword, restwords)

I''m sure the regular expression gurus here can come
up with something if it can be followed by other than
a space.

-Larry Bates
ha*****@gmail.com wrote:
我正在寻找最好和最有效的方法来替换str中的第一个单词
,如下所示:
aa to become - > / aa /成为
我知道我可以使用溢出而不是加入他们
但我也可以使用正则表达式
我确定有很多方法,但我需要真正的高效的
I am looking for the best and efficient way to replace the first word
in a str, like this:
"aa to become" -> "/aa/ to become"
I know I can use spilt and than join them
but I can also use regular expressions
and I sure there is a lot ways, but I need realy efficient one



" ha ***** @ gmail.com" <公顷***** @ gmail.com>写道:
"ha*****@gmail.com" <ha*****@gmail.com> writes:
我正在寻找最好和最有效的方法来替换str中的第一个单词
,如下所示:
aa to become - > / aa /成为
我知道我可以使用溢出而不是加入他们
但我也可以使用正则表达式
我确定有很多方法,但我需要真正的高效的
I am looking for the best and efficient way to replace the first word
in a str, like this:
"aa to become" -> "/aa/ to become"
I know I can use spilt and than join them
but I can also use regular expressions
and I sure there is a lot ways, but I need realy efficient one




假设你知道空格是空格,我喜欢找:


new =" / aa / " + old [old.find(''''):]


至于效率 - 我建议您调查timeit模块,并且

做一些测试数据代表你将采取的行动

正在使用。


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



Assuming you know the whitespace will be spaces, I like find:

new = "/aa/" + old[old.find('' ''):]

As for efficiency - I suggest you investigate the timeit module, and
do some tests on data representative of what you''re actaully going to
be using.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


10月20日, ha ***** @ gmail.com 写道:
On Oct 20, ha*****@gmail.com wrote:
我正在寻找最好和最有效的方法来取代第一个单词aa to be; - > / aa /成为
我知道我可以使用溢出而不是加入他们
但我也可以使用正则表达式
我确定有很多方法,但我需要真正的有效的
I am looking for the best and efficient way to replace the first word
in a str, like this:
"aa to become" -> "/aa/ to become"
I know I can use spilt and than join them
but I can also use regular expressions
and I sure there is a lot ways, but I need realy efficient one




当然有很多方法可以给这只猫皮肤;这里有一些试验。

timeit模块可用于比较(我认为我正在使用它

正确:-)。我认为字符串连接相当昂贵,所以它比%-formatting更快让我感到惊讶:



Of course there are many ways to skin this cat; here are some trials.
The timeit module is useful for comparison (and I think I''m using it
correctly :-). I thought that string concatenation was rather
expensive, so its being faster than %-formatting surprised me a bit:


这篇关于替换字符串中第一个单词的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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