python 中两个列表中的精确字符串匹配
exact string matching in two lists in python
我需要匹配两个列表,一个是字符,另一个是单词
我当前的代码是:
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']
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] = gop_scores.get(str(j))#this returns an integer value corresponding to char
sep_scores[str(word_p)] = scores
print(sep_scores)
当前输出为:-
*{'GUHD': {'G': 100.0, 'UH': 18.142809657631524, 'D': 61.62375099467158}, 'MAORNIHNG': {'M': 100.0, 'NG': 43.40719714138942}, 'SER': {'S': 100.0, 'ER': 100.0}, 'MAY': {'M': 100.0, 'AY': 100.0}, 'SEHLF': {'S': 100.0, 'EH': 89.72295878282416, 'L': 100.0, 'F': 0.0}, 'BAENGK': {'B': 7.166608749080874, 'AE': 68.10287800038276, 'NG': 43.40719714138942, 'K': 100.0}}}*
备注
一切似乎都很好,但没有与 MAORNIHNG 完全匹配的字符,我仍然得到{'M','NG'},我希望 MAORNIHNG 值为空
预期输出
*{'GUHD': {'G': 100.0, 'UH': 18.142809657631524, 'D': 61.62375099467158}, 'MAORNIHNG': {}, 'SER': {'S': 100.0, 'ER': 100.0}, 'MAY': {'M': 100.0, 'AY': 100.0}, 'SEHLF': {'S': 100.0, 'EH': 89.72295878282416, 'L': 100.0, 'F': 0.0}, 'BAENGK': {'B': 7.166608749080874, 'AE': 68.10287800038276, 'NG': 43.40719714138942, 'K': 100.0}}}*
'MAORNIHNG' 值应该是空字典
你只需要确保字典键组成了整个单词,如果不是 return 一个空字典,你可以通过加入它们并比较来做到这一点:
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}}
我需要匹配两个列表,一个是字符,另一个是单词 我当前的代码是:
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']
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] = gop_scores.get(str(j))#this returns an integer value corresponding to char
sep_scores[str(word_p)] = scores
print(sep_scores)
当前输出为:-
*{'GUHD': {'G': 100.0, 'UH': 18.142809657631524, 'D': 61.62375099467158}, 'MAORNIHNG': {'M': 100.0, 'NG': 43.40719714138942}, 'SER': {'S': 100.0, 'ER': 100.0}, 'MAY': {'M': 100.0, 'AY': 100.0}, 'SEHLF': {'S': 100.0, 'EH': 89.72295878282416, 'L': 100.0, 'F': 0.0}, 'BAENGK': {'B': 7.166608749080874, 'AE': 68.10287800038276, 'NG': 43.40719714138942, 'K': 100.0}}}*
备注
一切似乎都很好,但没有与 MAORNIHNG 完全匹配的字符,我仍然得到{'M','NG'},我希望 MAORNIHNG 值为空
预期输出
*{'GUHD': {'G': 100.0, 'UH': 18.142809657631524, 'D': 61.62375099467158}, 'MAORNIHNG': {}, 'SER': {'S': 100.0, 'ER': 100.0}, 'MAY': {'M': 100.0, 'AY': 100.0}, 'SEHLF': {'S': 100.0, 'EH': 89.72295878282416, 'L': 100.0, 'F': 0.0}, 'BAENGK': {'B': 7.166608749080874, 'AE': 68.10287800038276, 'NG': 43.40719714138942, 'K': 100.0}}}*
'MAORNIHNG' 值应该是空字典
你只需要确保字典键组成了整个单词,如果不是 return 一个空字典,你可以通过加入它们并比较来做到这一点:
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}}