Python列表推导式中使用if-else

data_list=[]

col=["a", "b", "c", "d"]
jdata={"a":1, "b":2, "c":3, "d":4}

for j in jdata:
    result=[ j[h] and j.setdefault(h) if h in j.keys() else None for h in col]
    data_list.append(tuple(result))
    print(result)

原文地址:https://www.cnblogs.com/hankleo/p/11804840.html