小编col*_*cab的帖子

如何以Pythonic方式将此元组列表转换为此dict?

我有一个元组列表

tuplist = [('person', u'English : 1, 2, 3 ; Dutch : 5, 6, 7'), ('home', u'English : 8, 9, 10; Dutch: 11, 12, 13')]
Run Code Online (Sandbox Code Playgroud)

我想将此转换为这个特定的词典

{'person': {u'Dutch': [u'5', u'6', u'7'], u'English': [u'1', u'2', u'3']}, 'home': {u'Dutch': [u'11', u'12', u'13'], u'English': [u'8', u'9', u'10']}}
Run Code Online (Sandbox Code Playgroud)

目前我这个:

dic = dict(tuplist)
final_dic = {}
for x in dic:
    str = dic[x]
    list1 = [y.strip() for y in str.split(';')]
    subdict = {}
    for z in list1:
        list2 = [y.strip() for y in z.split(':')]
        subdict[list2[0]] …
Run Code Online (Sandbox Code Playgroud)

python dictionary

1
推荐指数
1
解决办法
251
查看次数

标签 统计

dictionary ×1

python ×1