形成字典的名單我有一個python
其list
樣本數據是這樣的:從列表中蟒
list_str = ['1. Option 1', '2. Option 2 ', '3. Option 3', '4. Option 4']
現在我想要做的就是形成一個使用項目list of dictionary
從這個list
。輸出list of dictionary
應該是這樣的:
[
{
"text": "1. Option 1",
"value": "1"
},
{
"text": "2. Option 2",
"value": "2"
},
{
"text": "3. Option 3",
"value": "3"
},
{
"text": "4. Option 4",
"value": "4"
}
]
現在,我想這是第一步,形成list of dictionary
:
list_of_dict = dict((x,y) for x,y in enumerate(list_str,1))
產生這個輸出,這是不正確的輸出,但我不能進一步修改以獲得所需的輸出:
{1: '1. Option 1 ', 2: '2. Option 2 ', 3: '3. Option 3 ', 4: 'Option 4 '}
如何修改我的代碼以獲得正確的所需輸出?