我有一本字典,它看起来像:
dict = {'A':[1,2], 'B':[0], 'c':[4]}
需要它看起来像:
dict = {'A':[1,2], 'B':[0,0], 'c':[4,0]}
我现在在做什么:
dict = {x: y+[0] for (x,y) in dict.items() if len(y) < 2}
生成:
dict = {'B':[0,0], 'c':[4,0]}
知道如何避免淘汰那些不符合条件的人吗?
请您参考如下方法:
你快到了。尝试:
my_dict = {x: y + [0] if len(y) < 2 else y
for (x,y) in dict.items()}
(如 jp_data_analysis 所述,避免在 dict 等内置函数之后命名变量)
