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)

posted @ 2019-11-06 14:11  hank-li  阅读(1666)  评论(0编辑  收藏  举报