python - 将列表中的值附加到字典中

标签 python python-3.x list dictionary

我有带值的字典,我想根据列表和字典的键从列表中附加值

例如:

这是我的词典:

m_dict= {"Murder At Koh E Fiza" : "rot,r", "Subedar Joginder Singh" : "grn,4", "Blackmail" : "blau,9", "Rambo":"gelb,20"}

这是我的 list :

m_list=['Murder At Koh E Fiza','tt','Subedar Joginder Singh','yy','Blackmail','uu','Rambo','zz']

这是我想要作为输出的最终字典

 m_dict= {"Murder At Koh E Fiza" : "rot,r,tt", "Subedar Joginder Singh" : "grn,4,yy", "Blackmail" : "blau,9,uu", "Rambo":"gelb,20,zz"}

这是我正在尝试的代码:

for line in m_list:  
    m_dict.setdefault(m_dict, []).append(value)

但它对我不起作用。请协助。

最佳答案

m_dict= {"Murder At Koh E Fiza" : "rot,r", "Subedar Joginder Singh" : "grn,4", "Blackmail" : "blau,9", "Rambo":"gelb,20"}
m_list=['Murder At Koh E Fiza','tt','Subedar Joginder Singh','yy','Blackmail','uu','Rambo','zz']

for i,j in zip(m_list[::2], m_list[1::2]):
    m_dict[i] = '{},{}'.format(m_dict[i], j)
print m_dict

{'Murder At Koh E Fiza': 'rot,r,tt', 'Rambo': 'gelb,20,zz', 'Blackmail': 'blau,9,uu', 'Subedar Joginder Singh': 'grn,4,yy'}

关于python - 将列表中的值附加到字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49698314/

相关文章:

python - 无法使用 distutils 构建 Cython 模块

python - pandas 通过采样读取 csv

python - 登录认证系统

python - 如何重新排列这样的列表(python)?

python - 检查两个列表一致性的快速方法

python - 有条件地统计员工与经理的关系

python - 以桌面屏幕作为输入的实时 yolov5 检测

python-3.x - 运行时错误 : matplotlib does not support generators as input

mysql - 尝试通过Python(使用Pycharm)连接MySQL时,cursor()和connection()方法不起作用

c# - 创建一个包含对象新实例的列表