Chars to String的数组 [英] Array of Chars to String

查看:96
本文介绍了Chars to String的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在寻找一种很好的方法,只从字符串中获取那些字符串

在另一个字符串中并创建一个新字符串:

astr =" Bob Carol Ted Alice"
letters =" adB"
some_func(astr,letters)



" Bad"


我可以写这样的:


astr =" Bob Carol Ted Alice"

letters =" adB"


导入集

alist = [lttr for lttr in astr if lttr in set(letters)]

newstr =""

for lttr in alist :

newstr + = lttr


但这看起来很难看。我特别不喜欢newstr + = lttr因为它每次都会使新的字符串变为
。我认为像这样的东西必须已经在某个地方使用

功能,或者我可以使用

内置工具提高效率。


有什么想法吗?


James


-

James Stroud
加州大学洛杉矶分校基因组学和蛋白质组学研究所

专栏951570

洛杉矶,加利福尼亚州90095

http://www.jamesstroud.com/

解决方案

James Stroud< js ***** @ mbi.ucla.edu>写道:

但这看起来很难看。我特别不喜欢newstr + = lttr因为它每次都会成为一个新的字符串。我认为像这样的东西必须已经在某个地方使用
功能,或者我可以使用
内置工具提高效率。




"" .join


''as


James Stroud写道:

您好,

我正在寻找一种很好的方法,只能从字符串中取出那些字符串,这些字符串在另一个字符串中并创建一个新的字符串:

< blockquote class =post_quotes>

astr =" Bob Carol Ted Alice"
letters =" adB"
some_func(astr,letters)
"坏




astr =" Bob Carol Ted Alice"

letters =" adB"

both = [x代表x中的x,如果x代表字母]

打印两者


[''B'',''a'' ,''d'']



On Tue,2005年4月19日13:33:17 -0700,James Stroud< js ***** @ mbi.ucla.edu>写道:

你好,

我正在寻找一种很好的方法,只从字符串中取出那些字符串
在另一个字符串中并制作一个新的字符串:

astr =" Bob Carol Ted Alice"
letters =" adB"
some_func(astr (字母)坏

我可以这样写:

astr =" Bob Carol Ted Alice"
letters =" adB"

导入集
alist = [lttr for lttr in astr if lttr in set(letters)]
newstr =""
for lttr in alist:
newstr + = lttr

但这看起来很难看。我特别不喜欢newstr + = lttr因为它每次都会成为一个新的字符串。我认为像这样的东西必须已经在某个地方使用
功能,或者我可以使用
内置工具提高效率。

任何想法?



我认为如果要修改的字符串是_very_ long,这将是值得的:

def some_func( s,letters,table =''''。join([chr(i)for i in xrange(256)])):
... return s.translate(table,

...''''。join([chr(i)for x in xrange(256)if chr(i)not in letters])

... some_func(" Bob) Carol Ted Alice",''adB'')



''坏''


见帮助(str.translate)


如果你想在一个循环中使用它,使用相同的字母我想要消除重复的

计算删除。你可以创建一个返回函数的工厂函数

,它使用闭包单元格中的删除。但不要过早优化;-)


问候,

Bengt Richter


Hello,

I am looking for a nice way to take only those charachters from a string that
are in another string and make a new string:

astr = "Bob Carol Ted Alice"
letters = "adB"
some_func(astr,letters)


"Bad"

I can write this like this:

astr = "Bob Carol Ted Alice"
letters = "adB"

import sets
alist = [lttr for lttr in astr if lttr in Set(letters)]
newstr = ""
for lttr in alist:
newstr += lttr

But this seems ugly. I especially don''t like "newstr += lttr" because it makes
a new string every time. I am thinking that something like this has to be a
function somewhere already or that I can make it more efficient using a
built-in tool.

Any ideas?

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/

解决方案

James Stroud <js*****@mbi.ucla.edu> writes:

But this seems ugly. I especially don''t like "newstr += lttr" because it makes
a new string every time. I am thinking that something like this has to be a
function somewhere already or that I can make it more efficient using a
built-in tool.



"".join

''as


James Stroud wrote:

Hello,

I am looking for a nice way to take only those charachters from a string that
are in another string and make a new string:

astr = "Bob Carol Ted Alice"
letters = "adB"
some_func(astr,letters)
"Bad"



astr = "Bob Carol Ted Alice"
letters = "adB"
both = [x for x in astr if x in letters]
print both

[''B'', ''a'', ''d'']



On Tue, 19 Apr 2005 13:33:17 -0700, James Stroud <js*****@mbi.ucla.edu> wrote:

Hello,

I am looking for a nice way to take only those charachters from a string that
are in another string and make a new string:

astr = "Bob Carol Ted Alice"
letters = "adB"
some_func(astr,letters)"Bad"

I can write this like this:

astr = "Bob Carol Ted Alice"
letters = "adB"

import sets
alist = [lttr for lttr in astr if lttr in Set(letters)]
newstr = ""
for lttr in alist:
newstr += lttr

But this seems ugly. I especially don''t like "newstr += lttr" because it makes
a new string every time. I am thinking that something like this has to be a
function somewhere already or that I can make it more efficient using a
built-in tool.

Any ideas?

James


I think this will be worth it if your string to modify is _very_ long:

def some_func(s, letters, table=''''.join([chr(i) for i in xrange(256)])): ... return s.translate(table,
... ''''.join([chr(i) for i in xrange(256) if chr(i) not in letters]))
... some_func("Bob Carol Ted Alice", ''adB'')


''Bad''

see help(str.translate)

If you want to use it in a loop, with the same "letters" I''d want to eliminate the repeated
calculation of the deletions. You could make a factory function that returns a function
that uses deletions from a closure cell. But don''t optimize prematurely ;-)

Regards,
Bengt Richter


这篇关于Chars to String的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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