Python 数组转为字典

在Python中
id1=["name:gogo","age:13","sex:man"]
如何转为
id1={"name":"gogo","age":"13","sex":"man"}


回答:

python">myList = ["name:gogo", "age:13", "sex:man"]

myList = list(map(lambda s: s.split(":"), myList))

myDict = dict(myList)

Python 数组转为字典</p><p>


回答:

上好的语法糖倒是用起来

>>> l = ["name:gogo", "age:13", "sex:man"]

>>> dict([v.split(':') for v in l])

{'name': 'gogo', 'age': '13', 'sex': 'man'}


回答:

现学现用

list = ["name:gogo","age:13","sex:man"]

result = {}

for item in list:

temp = item.partition(":")

result[temp[0]] = temp[2]

print(result)

以上是 Python 数组转为字典 的全部内容, 来源链接: utcz.com/p/938109.html

回到顶部