python字典列表循环嵌套 合并多个列表元素
user2op = {
"aaa":[
"123",
"1334",
"124",
"234"],
"bbb":[
"1232",
"133",
"1241",
"1234"]
}
op2post = {
"123":[
"sadafsagf",
"wewrwr",
"dsff",
"sadaf"],
"234":[
"1faffa",
"safasfga",
"sdsf",
"zxzc"],
'1234': [
'zxcafssf',
'dadaf22424',
'dafasv',
'vxvdfw'],
"124":[
'eqeqe',
'qw1',
'dqdqf'
]
}
user2post={}
for user,ops in user2op.items():
posts = []
for op in ops:
temp = op2post.get(op)
if temp:
posts.extend(op2post.get(op))
user2post[user] = posts
print(user2post)
输出
"""
{'aaa': ['sadafsagf',
'wewrwr',
'dsff',
'sadaf',
'eqeqe',
'qw1',
'dqdqf',
'1faffa',
'safasfga',
'sdsf',
'zxzc'],
'bbb': ['zxcafssf', 'dadaf22424', 'dafasv', 'vxvdfw']}
"""