2 回答
TA贡献1816条经验 获得超6个赞
试试这个方法
for key, value in predefined_dict.items():
for word in predefined_list:
for n in range(word.count(key)):
final = word.replace(key,value,n+1)
if final not in predefined_list:
predefined_list.append(final)
输出:
>>> predefined_list
['gross pay', 'gross poy', 'grass pay', 'grass poy', 'cross pay', 'cross poy', 'crass pay', 'crass poy']
TA贡献1780条经验 获得超5个赞
只需通过遍历字典的键和值来替换字符串
modified_list = [predefined_list[0].replace(k,v) for k,v in predefined_dict.items()]
输出
['gross poy', 'grass pay', 'cross pay', 'gross pay']
相似地
predefined_list = ['pay gross', 'gross pay']
modified_dict = {list_k: [list_k.replace(k,v) for k,v in predefined_dict.items()]
for list_k in predefined_list}
输出
{'gross pay': ['gross poy', 'grass pay', 'cross pay', 'gross pay'],
'pay gross': ['poy gross', 'pay grass', 'pay cross', 'pay gross']}
添加回答
举报