是否有字典+列表理解方法可以做到这一点?

问题描述

我正在尝试使用字典和列表理解来解决以下问题。给定

input = {"a":["b","c"],"d":["b","e"],"f":["g","c"]}

预期输出为::

output = {"b":["a","d"],"c":["a","f"],"e":["d"],"g":["f"]}

到目前为止,我只能提出::

output = {}
for (k,v) in input.items():
    for x in v:
        if x[0] not in output:
            output[x[0]] = [k]
        else:
            output[x[0]].append(k)

单行字典+列表理解方法可以做到这一点吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)