找出字符串是否包含按特定顺序排列的字母组合



我正试图编写一个程序来查找英语中包含3个字母的单词,按顺序排列,但不一定连续。例如,字母组合EJS将输出单词EJectS等。你提供字母,程序输出单词。

然而,该程序没有按正确的顺序给出字母,并且完全不能使用双字母,如字母FSF或VVC。我希望有人能告诉我如何修复这个错误。

这是完整的代码:

with open("words_alpha.txt") as words:
wlist = list(words)
while True:
elim1 = []
elim2 = []
elim3 = []
search = input("input letters here: ")
for element1 in wlist:
element1 = element1[:-1]
val1 = element1.find(search[0])
if val1 > -1:
elim1.append(element1)
for element2 in elim1:
val2 = element2[(val1):].find(search[2])
if val2 > -1:
elim2.append(element2)
for element3 in elim2:
val3 = element3[((val1+val2)):].find(search[1])
if val3 > -1:
elim3.append(element3)
print(elim3)

您正在使这件事变得非常复杂。要测试一个单词是否按顺序包含字母E、J和S,可以将其与正则表达式E.*J.*S:进行匹配

>>> import re
>>> re.search('E.*J.*S', 'EJectS')
<_sre.SRE_Match object; span=(0, 6), match='EJectS'>
>>> re.search('E.*J.*S', 'JEt engineS') is None
True

因此,这里有一种简单的方法来编写一个测试任意字母组合的函数:

import re
def contains_letters_in_order(word, letters):
regex = '.*'.join(map(re.escape, letters))
return re.search(regex, word) is not None

示例:

>>> contains_letters_in_order('EJectS', 'EJS')
True
>>> contains_letters_in_order('JEt engineS', 'EJS')
False
>>> contains_letters_in_order('ABra Cadabra', 'ABC')
True
>>> contains_letters_in_order('Abra CadaBra', 'ABC')
False

如果你想测试单词表中的每个单词,那么值得先测试一次pattern = re.compile(regex),然后再测试每个单词的pattern.search(word)

您需要使用read()正确读取文件,由于每个单词之间都有一条换行符,因此调用split('n')以正确创建单词列表。逻辑很简单。如果所有字母都在单词中,则获取每个字母的索引,并检查索引的顺序是否与字母的顺序匹配。

with open('words_alpha.txt') as file:
word_list = file.read().split('n')
search = input("input letters here: ").lower()
found = []
for word in word_list:
if all(x in word for x in search):
i = word.find(search[0])
j = word.find(search[1], i + 1)
k = word.find(search[2], j + 1)
if i < j < k:
found.append(word)
print(found)

使用功能:

def get_words_with_letters(word_list, search):
search = search.lower()
for word in word_list:
if all(x in word for x in search):
i = word.find(search[0])
j = word.find(search[1], i + 1)
k = word.find(search[2], j + 1)
if i < j < k:
yield word
words = list(get_words_with_letters('fsf'))

代码的问题是,您在第一个循环中使用特定单词的val1来替换第二个循环中的另一个单词。因此,val1在大多数情况下都是错误的值,因为你在第一个循环中为第二个循环中的每个单词使用了最后一个单词中第一个字母的位置。

有很多方法可以解决你想要做的事情。然而,我下面的代码应该与你的解决方案相当接近。我试图解释评论中发生的一切:

# Read words from file
with open("words_alpha.txt") as f:
words = f.readlines()
# Begin infinite loop
while True:
# Get user input
search = input("Input letters here: ")
# Loop over all words
for word in words:
# Remove newline characters at the end
word = word.strip()
# Start looking for the letters at the beginning of the word
position = -1
# Check position for each letter
for letter in search:
position = word[position + 1:].find(letter)
# Break out of loop if letter not found
if position < 0:
break
# If there was no `break` in the loop, the word contains all letters
else:
print(word)

对于每个新的字母,我们从position + 1开始查找,其中position是先前找到的字母的位置。(这就是为什么我们必须做position = -1,所以我们开始在-1 + 1 = 0寻找第一个字母。(

理想情况下,应该将n的删除移到循环之外,因此必须执行一次,而不是每次搜索。为了与您的代码保持一致,我只是将其保留在循环中。

另外,顺便说一下,目前还没有大写/小写的处理。那么,例如,对abc的搜索是否应该与Abc不同?我不确定你在那里需要什么。

最新更新