如何迭代单词列表以查找具有某种模式的单词?

时间:2018-04-01 12:07:11

标签: python-3.x loops text

我想合并两个单词的标记城市名称,然后打印。如何向前移动迭代器并使用循环执行此操作?

sentence = "There are many cities. Random text, city name <c.first> New </c.first> <c.last> York </c.last> and text continues."
sentence = sentence.split()

#print(sentence)

for word in sentence:
    if(word == '<c.first>' ):
        print(word)
      # Here I want to be able to find New York, as single element and print. Output 'New York'

2 个答案:

答案 0 :(得分:1)

如果您只想在每个标记后打印出所有单词,那么您可以执行以下操作,其中tags是标记列表。

sentence = "There are many cities. Random text, city name <c.first> New </c.first> <c.last> York </c.last> and text continues."
sentence = sentence.split()

found_tag = False

tags = ['<c.first>', '<c.last>']

for word in sentence:
    if(word in tags):
        found_tag = True
    elif found_tag:
        print(word)
        found_tag = False

这将打印:

New
York

答案 1 :(得分:1)

感谢@Ollie的想法,我可以找到问题的解决方案。我想以某种方式将整个城市名称统计为一个实体。不确定这是否是一种有效的方式。所以,我仍然欢迎提出建议。

sentence = "There are many cities. Random text, city name <c.first> New City of </c.first> <c.last> York </c.last> and text continues.  <c.first> A LONG </c.first> <c.last> STRANGE CITY NAME </c.last>"
sentence = sentence.split()

found_tag = False

#tags = ['<c.first>', '</c.first>', '<c.last>','</c.last>']

opening_tags = ['<c.first>',  '<c.last>']
closing_tags = ['</c.first>', '</c.last>']

for word in sentence:
    if(word in opening_tags):
        found_tag = True
    elif found_tag and word not in closing_tags:
        print(word, end =' ')
    elif word in closing_tags:
        found_tag = False