我有以下 ['You and i','everyone else','cat and dog','We u all']
我需要以某种方式识别 and
和 u
旁边的字符串。
例如,我期望以下输出:
You
i
cat
Dog
We
all
基本上,每个句子应该从and
和u
拆分出来。我需要打印 and
和 u
两侧的两个文本。
我做的是错误的,但这是我的尝试之一:
sen = [w for w in words if re.search(r'.*and*.','.*u*.', w)]
for st in sen:
print st
最佳答案
遍历每一行。检测它是否有 and
或 u
。如果是,则将其拆分到该 token 上并最终打印。对于所有其他行忽略。
>>> sentences = ['You and i', 'everyone else', 'cat and dog', 'We u all']
>>> for line in sentences:
... if 'and' in line:
... for split_word in line.split('and'):
... print split_word.strip()
... elif ' u ' in line:
... for split_word in line.split(' u '):
... print split_word.strip()
... else:
... pass
...
You
i
cat
dog
We
all
>>>
关于python - 识别文本之间的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17990936/