English 中文(简体)
简单地处理
原标题:simple regex trouble
  • 时间:2012-05-12 06:17:21
  •  标签:
  • python
  • regex

我的语句

意见认为:

 0 0 0 0 1 0 0 0 1 1 0 0  

等等。

i 想要产出:

( 0 0 0 0 ,  1 0 0 0 ,  1 1 0 0 ) 

或每组4个数字是其本身的要素

至今

>>> truth = re.compile( (([0-1]D*?){4})* )
>>> truth.search( 0 0 0 0 1 0 0 0 ).groups()
( 0 0 0 0 ,  0 )

或若干类似情况,但没有任何进展。 这里的几件事情对我来说是新的,一米阅读了这套书,但似乎可以把分歧分割在一起。 我现在不说,为什么我最后拿到......。

最终,投入将有很多条块,但如果用于小案件,即肯定会将其转化为多余。

感谢

最佳回答

我不想对此使用定期的表述。 http://docs.python.org/library/itertools.html#recipes” rel=“noreferer”>recipes on the itertools 文件:

>>> [   .join(x) for x in grouper(4, truth.split())]

See it working online: ideone


此处为<代码>grouper的源代码。 (摘自 it鱼文件):

from itertools import izip_longest

def grouper(n, iterable, fillvalue=None):
    "grouper(3,  ABCDEFG ,  x ) --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)
问题回答

我不会说得太高,但你可以稍微改变一下,使用<条码>。

re.findall( (?:[0-1]s*){4} ,  0 0 0 0 1 0 0 0 1 1 0 0 )

这是:

>>> s= 0 0 0 0 1 0 0 0 1 1 0 0  
>>> [   .join(x) for x in zip(*[iter(  .join(s.split()))]*4)]
[ 0 0 0 0 ,  1 0 0 0 ,  1 1 0 0 ]

如果你想到底:

>>> tuple(   .join(x) for x in zip(*[iter(  .join(s.split()))]*4))
( 0 0 0 0 ,  1 0 0 0 ,  1 1 0 0 )

如果你真的想要获得许可:

>>> [x.strip() for x in re.findall(r (?:ds*){4} ,s)]
[ 0 0 0 0 ,  1 0 0 0 ,  1 1 0 0 ]

a 只是为了 fun而实现的cra解决办法:

import math
s =  0 0 0 0 1 0 0 0 1 1 0 0 
step = 8
result = [s[0+i*step:step+i*step] for i in xrange(int(math.ceil(float(len(s))/step)))]
print result




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签