将单个字典值转换为列表?

Paa*_*das 1 python dictionary

让我们拿字典

r = {u'a':'john',u'b':'ABA设计公司,发展思想基金会,食品网南海滩葡萄酒和美食节',u'c':'给'}

我想将其转换为:

r = {u'a':'john',u'b':['ABA Design House','开发思想基金','食物网南海滩葡萄酒和美食节'',u'c':'给' }

Red*_*ron 5

使用内置方法split()

>>> r[u'b'] = r[u'b'].split(',')
>>> r
    {u'a': 'john', u'c': 'give', u'b': ['ABA Design House ', ' Developing Minds Foundation ', ' Food Network South Beach Wine & Food Festival']}
Run Code Online (Sandbox Code Playgroud)

如果您对前导/尾随空格感到不舒服,请在拆分后使用strip()方法

>>> r[u'b']=[x.strip() for x in r[u'b'].split(',')]
>>> r
{u'a': 'john', u'c': 'give', u'b': ['ABA Design House', 'Developing Minds Foundation', 'Food Network South Beach Wine & Food Festival']}
Run Code Online (Sandbox Code Playgroud)