Monday, 15 July 2013

python - search inside one string and eliminate the found positions in an accompanying list -


I have a string with text and a list together, in which the first list contains information about each character. For example:

  text = "this, and this test is AMS" text INFO = [1, 4, 6, 7, 8, 3, 6, 2, 4, ... 7, 0]  

Where every position in the list refers to a character is the text, i.e. lane (text) == lane (text info) where the text is INFO [i] in ith The character information is in the text.

I want to end the examples of "this" in the text, and I want to eliminate those letters (i.e. 4 terms referenced in the list)

  tmpText = Text tmpTextInfo = textInfoM = re  

Search ("this", tmpText) while m: tmpText = tmpText [0: m.start ()] + tmpText [m.end ():] tmpTextInfo = tmpTextInfo [0: m.start ()] + tmpTextInfo [m. End ():] m = re.search ("this", tmptext) text = tmptext textInfo = tmpTextInfo

It works and receives what I hope for example: If the input is

  text = "this test is it" textInfo = [0,1,2,3,4,5,6,7,8,9,10, 11,12, 13,14,15,16]  

Then the resulting text and text will be infra string

  text = "test is" textInfo = [4, 5 , 6, 7, 8, 9, 14, 15, 16]  

But this does not sound like a dragon at all, and I Sector sure there is more compact and efficient way to do it, what is it?

OK, I do not think that if you actually have a regular expression You can simply put the list of stars in the store and connect with them at the end but it will not improve the readability.

If the problem is about removing the tokens different from the space or punctuation marks, then you generate the generator for a pair of tokens with the related textInfo After that you can filter by token (or attached information if you want) and you can add both lists again. But I'm not sure that it will actually bring anything.


No comments:

Post a Comment