字符串到字典

sds*_*das 1 python

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

Milk, Cheese, Bottle
Run Code Online (Sandbox Code Playgroud)

该程序将其转换为字典.

{"Milk":"NULL", "Cheese":"NULL", "Bottle":"NULL"}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

Joh*_*ica 7

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


zxt*_*zxt 5

>>> s = "Milk, Cheese, Bottle"
>>> d = dict.fromkeys(s.split(', '),"NULL")
>>> d
{'Cheese': 'NULL', 'Milk': 'NULL', 'Bottle': 'NULL'}
Run Code Online (Sandbox Code Playgroud)