我的意思是以某种方式将此伪代码转换为python3代码:
dict(itemName = itemRecipe)
到目前为止我尝试了什么:
1)
allItems = [{}]
itemName = 'Pancake'
itemRecipe = ['Eggs', 'Flour']
allItems[0].update(dict(itemName = itemRecipe))
这样就产生了一个数组,并且有一个带有键的对象'itemName',而不是'Pancake'.
2)
allItems = [{}]
itemName = 'Pancake'
itemRecipe = ['Eggs', 'Flour']
allItems[0].update(dict(locals()[itemName] = itemRecipe))
扔我一个SyntaxError: keyword can't be an expression.
我无所适从.也许有人可以帮助我吗?
 allItems.update({itemName: itemRecipe})
但是在你的代码中allItems(btw camelcase在Python中并不公平)是一个List,所以你应该追加它
 allItems.append({itemName: itemRecipe})
如果你想让allItems成为一个dict,你可以这样做
 allItems = {}
 allItems[itemName] = itemRecipe
| 归档时间: | 
 | 
| 查看次数: | 55 次 | 
| 最近记录: |