2

我想知道如何通过索引来导航 finditer 正则表达式操作产生的对象。

我的字符串是s = "fish oil X22 stack peanut C4"

这是我的代码:

import re
words = re.finditer('\S+', s)
has_digits = re.compile(r'\d').search
for word in words:
    if has_digits(word.group()):
        print (the word that is two words back)

期望的输出 =

fish
stack
4

2 回答 2

4

您可以使用 adeque来保存元素。然后这变得很容易:

import re
from collections import deque
s = 'fish oil X22 stack peanut C4'
words = re.finditer('\S+', s)
has_digits = re.compile(r'\d').search
deq = deque([],2)
for word in words:
    wordtxt = word.group()
    if has_digits(wordtxt):
        print (deq[0])
    deq.append(wordtxt)

有点不清楚字符串应该发生什么:

s = 'fish oil X22 stack C4'

它应该打印“fish”和“oil”还是“fish”和“X22”。另外,如果第一个子字符串是“X22”怎么办?在我的回答中,这会导致IndexError,但很难知道你想用它做什么......

于 2013-04-19T17:15:48.700 回答
1

您可以使用itertools.teeitertools.izip

import re
import itertools as it

s = "fish oil X22 stack peanut C4"
words = re.finditer('\S+', s)
has_digits = re.compile(r'\d').search
words, words_copy = it.tee(words)
next(words); next(words)       #Skip the first two words of one iterator
for word, back_word in it.izip(words, words_copy):
    if has_digits(word.group()):
            print(back_word.group())
于 2013-04-19T20:30:24.427 回答