Python 匹配和连接列表中词典的键/val

Python 匹配和连接列表中词典的键/val,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,这是一个列表,我有这样的列表: k = [{a:1}, {b:2}, {c:3}, {d:4}, {a: 5}] and t = [{a:6}, {b:7}, {c:8}, {d:9}, {a: 10}] 如果a在k中的字典键与t在t中的字典键匹配,则提取值并在字典中键入 然后我想重组如下: newlist = [a is 1 and 6, b is 2 and 7, c is 3 and 8, d is 4 and 9, a is 5 and 10] 从你的描述中,不难理解你到底需要什么

这是一个列表,我有这样的列表:

k = [{a:1}, {b:2}, {c:3}, {d:4}, {a: 5}] and t = [{a:6}, {b:7}, {c:8}, {d:9}, {a: 10}] 
如果
a
k
中的字典键与
t
t
中的字典键匹配,则提取值并在字典中键入 然后我想重组如下:

newlist = [a is 1 and 6, b is 2 and 7, c is 3 and 8, d is 4 and 9, a is 5 and 10]

从你的描述中,不难理解你到底需要什么,但我已经尽了最大努力来获得我认为你需要的东西!:-)

这将产生:

newlist = [{'a': [1, 6]}, {'b': [2, 7]}, {'c': [3, 8]}, {'d': [4, 9]}, {'a': [5, 10]}]

注意,由于重复的键,我没有合并字典

提示:阅读有关
defaultdict
的内容。为什么不告诉我们您尝试了什么以及为什么它起作用或不起作用?两个列表中都有两个词典,它们都有一个
a
条目。此外,列表的语法只有在存在变量
a
b
c
d
时才有效,否则正确的语法是,例如
{'a':1}
,这是有效的,thanks@nayang-感谢您的反馈!如果您能够投票支持此解决方案,那就太好了-如果您还没有:-)
newlist = [{'a': [1, 6]}, {'b': [2, 7]}, {'c': [3, 8]}, {'d': [4, 9]}, {'a': [5, 10]}]