繁体   English   中英

SyntaxError:列表中的语法无效

[英]SyntaxError: invalid syntax in list

我想创建一个常量变量,其结构如下所示:

 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__();

主要任务是我想创建一个函数“解决”,它使用“数量”和“空间”来查找包装所需的最小袋子。 我想为此使用背包法。

正如 Shahroozevsky 已经提到的那样,您必须删除 const。 字典由键:值对组成。 密钥必须是不可变的。 这意味着您可以使用字符串、数字或元组作为字典键。

line_items:[ { "title": 'cookie', "quantity": 5, "space": 0.5 },

https://www.tutorialspoint.com/python/python_dictionary.htm

一个有效的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM