小编lyr*_*pha的帖子

Python如何用字典中的值替换一个列表中的值?

例子:

\n\n

我的清单是['tree'\xef\xbc\x8c'world'\xef\xbc\x8c'tre'\xef\xbc\x8c'worl']

\n\n

我的字典是{'tre':'good','worl':nice}

\n\n

我的脚本:

\n\n
def replace(list, dictionary):\n    for i in list:\n        for k in dictionary:\n            list = list.replace(k, dictionary[k])\n    return list\nprint replace(input_file,curaw_dict)\n
Run Code Online (Sandbox Code Playgroud)\n\n

但每次我收到的结果都是像\xef\xbc\x9a

\n\n
goode\nniced\ngood\nnice\n
Run Code Online (Sandbox Code Playgroud)\n\n

我怎样才能让它更准确\n让它像

\n\n
tree\nworld\ngood\nnice\n
Run Code Online (Sandbox Code Playgroud)\n\n

多谢

\n

python dictionary replace list

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

dictionary ×1

list ×1

python ×1

replace ×1