DeTechn Blog

将字符串 "k:1 |k1:2|k2:3|k3:4",处理成字典 {k:1,k1:2,...}

str1 = "k:1|k1:2|k2:3|k3:4"
def str2dict(str1):
    dict1 = {}
    for iterms in str1.split('|'):
        key,value = iterms.split(':')
        dict1[key] = value
    return dict1
#字典推导式
d = {k:int(v) for t in str1.split("|") for k, v in (t.split(":"), )}

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »