regex - Grep multi-layered iterable for strings that match (Python) -


Say we have a multilayered iterable with some strings at the "final" level, yes the string is dynamic, but I think Is that you get my meaning:

  ['some', ('def', ('diff', 'udiff'), ('* .diff', '* .patch '(' 'Text / X-Difference' ',' Text / X-Patch ')), (' Delphi ', (' Delphi ',' Pass ',' Pascal ',' Object Skull '), (' *. Pass ', (' text / x-pascal ', [' walk ',' put one '],)), (' javascript + maco ', (' js + maco ',' javascript + mac '), (' App / X ...)  

Is there a convenient way to implement that? A search that will give me the index of match strings? I want something that Such a thing will work (where the list above is data ):

  gt; grep ('javascript', data)  

And this [[2,1,1], (2,2,0), (2,2,1), (2, 2, 2)] Perhaps I can not find a comparable solution Nothing returns like this, but I have multi strings of strings The wire can help in finding some strings within the list .... wire

I wrote a little but it seemed juvenile and stupid, so I thought I would ask here. I think I can put exception as a nest victim, the way I started on the basis of that level, which supported the function, but I hope to get some clean, abstract, dragon having had.

  Import re def rgrep (s, data): A footnote or string of strings is given a footnote, it gives the index / index of the string which includes the search string is. Args :: s - The string that you are searching for data - iterables of strings or strings of iterables' '' result = [] expr = re.compile (s) for data in data: try: Match = expr search (item) match! = None: TypeError except the results.append (Data.index (item)): Try the item in: try: m = expr.search (t) if m! = None: results.append ((List.index (item), item.index (t))) except TypeError: '' 'You can only go deeper! '' 'Near Return Returns''  

I split the recurring calculation with grepping: For the index, calculation in the item (IARR): ifinstance (item, basestring): yield (base + (index,)), item other: Def Numerate _Recsive (IARAR, base = ()): < Index for index (base, (base + (index,)): pair deaf grep_index (filt, iter): return (in index text, in text in ITR) P> In this way you can Non repetitive Recording and recurring can grepping:

  l = list (grep_index ('opt1', enumerate (sys.argv))) # Non recursive R = list (Grep_index ('diff', enumerate_recursive (Your_data)) # Recurring  

Also note that we are using an iterator here, if necessary, save the RAM for more sequentials.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -