8.将字符串 "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(":"), )}
原文地址:https://www.cnblogs.com/Fiona-Y/p/10567255.html
时间: 2024-10-06 09:44:57