相关疑难解决方法(0)

将冒号分隔的列表转换为字典?

我写了这样的东西来将逗号分隔的列表转换为字典。

def list_to_dict( rlist ) :
    rdict = {}
    i = len (rlist)
    while i:
        i = i - 1
        try :
            rdict[rlist[i].split(":")[0].strip()] = rlist[i].split(":")[1].strip()
        except :
            print rlist[i] + ' Not a key value pair'
            continue


    return rdict
Run Code Online (Sandbox Code Playgroud)

有没有办法

for i, row = enumerate rlist
    rdict = tuple ( row ) 
Run Code Online (Sandbox Code Playgroud)

或者其他的东西?

python dictionary list

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

dictionary ×1

list ×1

python ×1