综合编程

Search for substrings with an operator in a list of words in Python

微信扫一扫,分享到朋友圈

Search for substrings with an operator in a list of words in Python
0

I am attempting to run a program that will find a new value for A if there is a period contained within a certain index range of it. The problem is, in order to determine the domain of the search, it is necessary to have the words separated within a list, such as this (‘A’, ‘B.’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’). The object is to get the program to find the first A, search the smaller list (‘A B. C D’) for a ‘.’ and, if it finds one, to assign the variable to the next instance of A. All of the code works so far, except for finding the ‘.’ with an in operator, because it is treating it as ‘B.’ . This would not work in application, as ‘B’ wil not be known.

Is there any way to search for a ‘.’ even if it appears next to unknown text? I have attempted using regular expressions, but it is not responding well. This is probably a result of an error on my part.

words = 'A B. C D E F G A B C D E F G A B C D E F G A B C D E F G'
corpus = words.split()
index_A = corpus.index('A')
while '.' in corpus[index_A : index_A + 3]:
  # This does not seem to be picking up the '.' in 'b.' in isolation.  It will find
  # 'B.' but not '.' from corpus.
  index_A = corpus.index('A', index_A+1)
阅读原文...

Hello, buddy!

Strategic Insights into ESPN’s Success in the Digital Video Era

上一篇

These Custom iPhone X Models Made From Marble, Alligator Leather, or Sapphire Glass Cost Up...

下一篇

您也可能喜欢

评论已经被关闭。

插入图片
Search for substrings with an operator in a list of words in Python

长按储存图像,分享给朋友