如何使用迭代器检查python中字符串的后续元素?

2024-04-20 05:49:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想分析一个句子来检查一些条件:

a) If there is a period and it is followed by a whitespace followed by a lowercase letter

b) If there is a period internal to a sequence of letters with no adjacent whitespace (i.e. www.abc.com)

c) If there is a period followed by a whitespace followed by an uppercase letter and preceded by a short list of titles (i.e. Mr., Dr. Mrs.)

目前,我正在遍历字符串(行)并使用next()函数来查看下一个字符是空格还是小写等,然后我只是在行中循环。但是我该如何检查下一个、下一个角色是什么呢?我怎样才能找到以前的呢?你知道吗

line = "This is line.1 www.abc.com. Mr."

t = iter(line)
b = next(t)

for i in line[:len(line)-1]:
    a = next(t)
    if i == "." and (a.isdigit()): #for example, this checks to see if the     value after the period is a number
         print("True")

任何帮助都将不胜感激。非常感谢。你知道吗


Tags: andoftobyifiswwwline
2条回答

您可以使用多个后续操作来获取更多数据

line = "This is line.1 www.abc.com. Mr."

t = iter(line)
b = next(t)

for i in line[:len(line)-1]:
    a = next(t)
    c = next(t)
    if i == "." and (a.isdigit()): #for example, this checks to see if the     value after the period is a number
         print("True")

您可以通过将迭代保存到临时列表来获取以前的迭代

正则表达式是您想要的。你知道吗

由于要检查字符串中的模式,因此可以通过re库利用python对正则表达式的内置支持。你知道吗

示例:

#To check if there is a period internal to a sequence of letters with no adjacent whitespace 
import re
str = 'www.google.com'
pattern = '.*\..*'
obj = re.compile(pattern)
if obj.search(str):
    print "Pattern matched"

类似地,为要签入字符串的条件生成模式。你知道吗

#If there is a period and it is followed by a whitespace followed by a lowercase letter
regex = '.*\. [a-z].*'

您可以使用this简单工具在线生成和测试正则表达式

更广泛地阅读rehere

相关问题 更多 >