我有一个dict1如下所示:{"A":["a","b","c"],"B":["b","d","e"],"C":["a","e"]}
我的目标是编写一个将listA作为输入的函数。
例如,listA可以是[“ a”,“ b”,“ c”,“ e”]。
现在,我想返回一个dict2,其中dict1中的所有元素都包括在listA中。
输出应如下所示:{'A':["a","b","c"],"C":["a","e"]}
我的代码如下所示:
def func(listA: list) -> dict:
return set(x for x in dict1 if all(x in listA for x in dict1[x]))
我的输出仅返回键,我该怎么办?
最佳答案
dict1 = {"A":["a","b","c"],"B":["b","d","e"],"C":["a","e"]}
listA = ["a","b","c","e"]
dict2 = {key: l for key, l in dict1.items() if all(val in listA for val in l)}
关于python - 如何以列表作为值返回字典,但只返回键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60143891/