1 回答

TA貢獻1869條經驗 獲得超4個贊
您只需要確保字典鍵構成整個單詞,如果不返回空字典,您可以通過連接它們并比較來做到這一點:
char_list=['G','UH','D','S', 'ER', 'M', 'AY', 'S', 'EH', 'L', 'F', 'B', 'AE', 'NG', 'K']
word_list=['GUHD','MAORNIHNG','SER','MAY','SEHLF','BAENGK']
sep_scores={}
for word_p in word_list:
scores = {}
p_s=""
if str(word_p) != "NULL":
print(word_p)
for j in char_list:
if len(word_p) == len(p_s):
print("word end")
for i in scores.keys():
char_list.remove(i)
break
else:
if j in word_p:
p_s=p_s+j
scores[j] = 1#this returns an integer value corresponding to char
if ''.join(scores.keys()) == word_p:
sep_scores[str(word_p)] = scores
else:
sep_scores[str(word_p)] = {}
print(sep_scores)
>>> {'GUHD': {'G': 1, 'UH': 1, 'D': 1}, 'MAORNIHNG': {}, 'SER': {'S': 1, 'ER': 1}, 'MAY': {'M': 1, 'AY': 1}, 'SEHLF': {'S': 1, 'EH': 1, 'L': 1, 'F': 1}, 'BAENGK': {'B': 1, 'AE': 1, 'NG': 1, 'K': 1}}
添加回答
舉報