我想创建一个常量变量,其结构如下:
const example_order_3_lines = [
{
order_id: 111_222_333,
line_items:[
{
title: 'cookie',
quantity: 5,
space: 0.5,
},
{
title: 'md meal',
quantity: 3,
space: 2.0,
},
{
title: 'lg meal',
quantity: 4,
space: 3.0,
},
{
title: 'soup',
quantity: 2,
space: 2.5,
},
{
title: 'apple pie',
quantity: 3,
space: 1,
}
],
},
];
但是当我运行该单元格时出现此错误:
File "<ipython-input-60-44a3630eacc1>", line 1
const example_order_3_lines = [
^
SyntaxError: invalid syntax
当我从变量中删除const时,会出现此错误:
NameError Traceback (most recent call last)
<ipython-input-64-a12c8a34587f> in <module>
1 example_order_3_lines = [
2 {
----> 3 order_id: 111_222_333,
4 line_items:[
5 {
NameError: name 'order_id' is not defined
有人可以帮我解决这个问题吗?
有我的主要功能:
function __main__() {
const BAG_SIZE = 4;
const results = solve(example_order_3_lines, BAG_SIZE);
console.log('results: ', results[0].bags.length, JSON.stringify(results[0].bags, null, 2));
}
__main__();
主要任务是我想创建一个函数“ solve”,在其中使用“数量”和“空间”查找包装所需的最小袋子。我想为此使用背包方法。
答案 0 :(得分:3)
您必须按照Shahroozevsky所述删除const。字典由key:value对组成。密钥必须是不变的。这意味着您可以将字符串,数字或元组用作字典键。
line_items:[ { “ title”:“ cookie”, “数量”:5 “空格”:0.5 }
答案 1 :(得分:3)
有效的Python示例应如下所示:
example_order_3_lines = [
{
"order_id": "111_222_333",
"line_items": [
{
"title": 'cookie',
"quantity": 5,
"space": 0.5,
},
{
"title": 'md meal',
"quantity": 3,
"space": 2.0,
},
{
"title": 'lg meal',
"quantity": 4,
"space": 3.0,
},
{
"title": 'soup',
"quantity": 2,
"space": 2.5,
},
{
"title": 'apple pie',
"quantity": 3,
"space": 1,
}
],
},
]
print(example_order_3_lines[0]['line_items'][0]['title'])
输出:cookie