【问题标题】:SyntaxError: invalid syntax in listSyntaxError:列表中的语法无效
【发布时间】:2020-09-28 12:57:09
【问题描述】:

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

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

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

【问题讨论】:

  • 谁告诉你Python有const
  • 还有 111_222_333 很奇怪
  • 不错的@dejdej
  • 你从哪里得到这个的?这不是有效的 Python 语法。
  • 这看起来就像您将一堆其他语言复制粘贴到 Python 程序中并期望它能够运行。 (也许是 Typescript 什么的。)

标签: python list algorithm dictionary


【解决方案1】:

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

line_items:[ { “标题”:“饼干”, “数量”:5, “空间”:0.5 },

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      相关资源
      最近更新 更多