Skip to main content
 首页 » 编程设计

python之字符串到字典

2025年05月04日19xing901022

我正在尝试将字符串转换为字典。示例:

Milk, Cheese, Bottle 

程序会将其转换为字典。

{"Milk":"NULL", "Cheese":"NULL", "Bottle":"NULL"} 

我该怎么做?

请您参考如下方法:

>>> string = 'Milk, Cheese, Bottle' 
>>> dict((key, None) for key in string.split(', ')) 
{'Cheese': None, 'Milk': None, 'Bottle': None}