多个分隔符上的s.split() [英] s.split() on multiple separators

查看:125
本文介绍了多个分隔符上的s.split()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


好​​的,所以我想使用列表中的几个不同的

分隔符将字符串c拆分为单词(dels)。


我可以用以下类似C的方式执行此操作:


>> c =''abcde abc cba fdsa bcd''。split()
dels =''ce''
for j in dels:



cp = []
$ x $ b for x in xrange(0,len(c)-1):

cp.extend(c [i] .split(j))

c = cp


>> c



[''ab'','' d'','''',''ab'','''','''']


但是。当然有更多的Pythonic方法吗?


我不能这样做:


>> for i in dels:



c = [x.split(i )for c in c]


因为x.split(i)是一个列表。

解决方案

2007年9月30日, mr****@gmail.com < mr **** @ gmail.comwrote:


大家好,


好​​的,所以我想把一个字符串c分成几个不同的单词

列表中的分隔符(dels)。



看看这个食谱:

http://aspn.activestate.com/ASPN/Coo.../Recipe/303342


包含几种解决问题的方法。你可以两个

将你的所有分隔符翻译成一个,然后拆分它,

或(可能更简单的解决方案)进行列表理解

解决方案。


francesco


2007年9月30日, mr **** @ gmail.com < mr **** @ gmail.comwrote:


大家好,


好​​的,所以我想使用列表中的几个不同的

分隔符将字符串c拆分成单词(dels)。



看看这个食谱:

http://aspn.activestate.com/ASPN/Coo.../Recipe/303342


包含几种解决问题的方法。你可以两个

将你的所有分隔符翻译成一个,然后拆分它,

或(可能更简单的解决方案)进行列表理解

解决方案。


francesco


好的,所以我想用几个不同的$将字符串c拆分成单词b $ b


列表中的分隔符(dels)。


我可以通过以下类似C的方式执行此操作:


>>> c =''abcde abc cba fdsa bcd''。split()
dels =''ce ''对于j in dels:



cp = []
$ x $ b for x in xrange(0, len(c)-1):

cp.extend(c [i] .split(j))

c = cp


>>> c



[''ab' ','''','''',''ab' ',''''''''''



鉴于你原来的字符串,我不知道那是怎么回事

预期的结果拆分c对dels中的字符。


虽然有一些pythonistas派系不值得尊重

正则表达式(或至少发现它们被过度使用/误用,

我当然同意),他们可以很好地服务你的

目的:< br>


>> c =''abcde abc cba fdsa bcd''
import re
r = re.compile(''[ce]'')
r.split(c)



['''',''ab'','d'','''',''ab'','''',''''''''''''' 'fdsa'',''b'','d'',''''


假设regexp对象有一个split()方法。


-tkc


Hello everyone,

OK, so I want to split a string c into words using several different
separators from a list (dels).

I can do this the following C-like way:

>>c='' abcde abc cba fdsa bcd ''.split()
dels=''ce ''
for j in dels:

cp=[]
for i in xrange(0,len(c)-1):
cp.extend(c[i].split(j))
c=cp

>>c

[''ab'', ''d'', '''', ''ab'', '''', '''']

But. Surely there is a more Pythonic way to do this?

I cannot do this:

>>for i in dels:

c=[x.split(i) for x in c]

because x.split(i) is a list.

解决方案

On 9/30/07, mr****@gmail.com <mr****@gmail.comwrote:

Hello everyone,

OK, so I want to split a string c into words using several different
separators from a list (dels).

Have a look at this recipe:

http://aspn.activestate.com/ASPN/Coo.../Recipe/303342

which contains several ways to solve the problem. You could both
translate all your separators to a single one, and then split over it,
or (maybe the simpler solution) going for the list comprehension
solution.

francesco


On 9/30/07, mr****@gmail.com <mr****@gmail.comwrote:

Hello everyone,

OK, so I want to split a string c into words using several different
separators from a list (dels).

Have a look at this recipe:

http://aspn.activestate.com/ASPN/Coo.../Recipe/303342

which contains several ways to solve the problem. You could both
translate all your separators to a single one, and then split over it,
or (maybe the simpler solution) going for the list comprehension
solution.

francesco


OK, so I want to split a string c into words using several different

separators from a list (dels).

I can do this the following C-like way:

>>>c='' abcde abc cba fdsa bcd ''.split()
dels=''ce ''
for j in dels:

cp=[]
for i in xrange(0,len(c)-1):
cp.extend(c[i].split(j))
c=cp

>>>c

[''ab'', ''d'', '''', ''ab'', '''', '''']


Given your original string, I''m not sure how that would be the
expected result of "split c on the characters in dels".

While there''s a certain faction of pythonistas that don''t esteem
regular expressions (or at least find them overused/misused,
which I''d certainly agree to), they may be able to serve your
purposes well:

>>c='' abcde abc cba fdsa bcd ''
import re
r = re.compile(''[ce ]'')
r.split(c)

['''', ''ab'', ''d'', '''', ''ab'', '''', '''', ''ba'', ''fdsa'', ''b'', ''d'', '''']

given that a regexp object has a split() method.

-tkc


这篇关于多个分隔符上的s.split()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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