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

2020-01-31T09:54:02
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(":"), )}
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »