我正在尝试编写一个程序,该程序将使用指定的字母和通配符('*'
)并根据列表中的单词检查它们以打印所有可用的匹配项。我编写了下面的代码,当使用两个通配符时,它将起作用:
def wildcard_search(letters, count):
alpha = 'abcdefghijklmnopqrstuvwxyz'
words = ['hello', 'hi', 'good', 'help', 'hellos', 'helloing', 'hallow', 'no']
count = 0
wild_loc = []
while count < len(letters):
for letter in letters:
if letter == '*':
wild_loc.append(count)
count += 1
for letter in alpha:
new_letters = letters[:wild_loc[1]].replace('*', letter)
for each in words:
each = each.strip('')
if new_letters in each:
holder = new_letters
for letter in alpha:
new_letters = letters[wild_loc[1]:].replace('*', letter)
for each in words:
each = each.strip('')
if holder + new_letters in each:
print each
我的问题是,当使用两个以上的通配符时,如何编写此代码以返回结果?我尝试使用下面的while循环,但最终我的索引超出了范围错误:
count = 0
store = ''
while count <= len(wild_loc)-1:
for letter in alpha:
if count != len(wild_loc) - 1:
new_letter = letters[:wild_loc[count]].replace('*', letter)
for each in words:
each = each.strip('')
if new_letter in each:
res = store + new_letter
store = new_letter
count += 1
elif count == len(wild_loc) - 1:
new_letter = letters[wild_loc[count]:].replace('*', letter)
for each in words:
each = each.strip('')
if (res + new_letter) in each:
print each
count += 1