【问题标题】:Python how to replace values in one list with values in a dictionary?Python如何用字典中的值替换一个列表中的值?
【发布时间】:2017-12-25 14:10:09
【问题描述】:

示例:

我的名单是['tree','world','tre','worl']

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

我的脚本:

def replace(list, dictionary):
    for i in list:
        for k in dictionary:
            list = list.replace(k, dictionary[k])
    return list
print replace(input_file,curaw_dict)

但是每次我收到的结果都是这样的:

goode
niced
good
nice

我怎样才能使它更准确 让它像

tree
world
good
nice

非常感谢

【问题讨论】:

  • 不要使用list 作为变量名。它把同名的函数压缩了
  • 我不相信你已经运行了你输入的代码。 list 没有名为 replace 的方法。请创建尽可能短的程序来准确演示错误并将该程序复制粘贴(从不重新输入)到您的问题中。请参阅minimal reproducible example 了解更多信息。
  • 第一个列表中的逗号是什么?

标签: python list dictionary replace


【解决方案1】:
>>> li=['tree', 'world', 'tre', 'worl']
>>> di={'tre':'good','worl':'nice'}
>>> print('\n'.join(di.get(e,e) for e in li))
tree
world
good
nice

【讨论】:

  • 感谢您提供此代码 sn-p,它可能会提供一些即时帮助。一个正确的解释would greatly improve 其教育价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有类似但不相同的问题的读者更有用。请edit您的答案添加解释,并说明适用的限制和假设。
【解决方案2】:

for k in dictionary: 不是迭代字典中项目的正确方法。相反,您应该使用enumerate 来遍历列表中的项目并在字典中查找它们:

def replace(lst, dictionary):
    for k,v in enumerate(lst):
        if v in dictionary:
            lst[k] = dictionary[v]
    return lst

对于列表中的每一项,k 是值的索引,v 是值本身。然后检查该值是否在字典中,如果是,则将列表中的值替换为字典中的值。

您也不应该将变量命名为list,因为这是 Python 中的保留字。

您也可以使用列表推导:

def replace(lst, dictionary):
    return [item if not item in dictionary else dictionary[item] for item in lst]

【讨论】:

  • 几点说明: 1) list 不是保留字。它只是内置的类名,和任何其他名称一样,它会被其他作用域遮蔽。 2)for k in dictionary 是遍历字典的正确方法;在这种情况下,这不是最好的。
【解决方案3】:

让我们改为列表推导式。

replaced_list = [x if x not in my_dict else my_dict[x] for x in my_list]

我想如果你想要一个你可以做的功能:

replace = lambda my_dict, my_list: [x if x not in my_dict else my_dict[x] for x in my_list]

def replace(my_list, my_dict):
    return [x if x not in my_dict else my_dict[x] for x in my_list]

【讨论】:

    【解决方案4】:
    input_file = ['tree', 'world', 'tre', 'worl']
    curaw_dict = {'tre':'good','worl':'nice'}
    
    def replace(list, dictionary):
        return [curaw_dict.get(item, item) for item in list]
    print replace(input_file,curaw_dict)
    

    【讨论】:

      【解决方案5】:

      'key' in dictionary 是检查字典中是否存在键的方法:

      def replace(list, dictionary):
          new_list = []
      
          for i in list:
              if i in dictionary:
                  new_list.append(dictionary[i])
              else:
                new_list.append(i)
      
          return new_list
      

      【讨论】:

        【解决方案6】:
        def replace(list, dictionary):
            for idx, val in enumerate(list):
                if i in k:
                   list[idx] = dictionary[list[idx]]
            return list
        print replace(input_file,curaw_dict)
        

        您不需要遍历字典。 Replace 进行部分替换,但in 将检查字典中是否存在键。

        【讨论】:

          猜你喜欢
          • 2010-11-07
          • 1970-01-01
          • 2021-12-12
          • 1970-01-01
          • 2021-07-13
          • 2019-02-23
          • 2021-03-11
          • 1970-01-01
          • 2023-03-10
          相关资源
          最近更新 更多