简体   繁体   中英

SyntaxError: invalid syntax in list

I want to create a constant variable whose structure looking like this:

 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,
      }
    ],
  },
];

but when I run that cell I got this error:

 File "<ipython-input-60-44a3630eacc1>", line 1
    const example_order_3_lines = [
                              ^
SyntaxError: invalid syntax

when i remove const from the variable it give this error:

   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

Can someone help me what is wrong in this.

There is my main function:

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

The main task is that I want to create a function 'solve' where it use 'quantity' and 'space' to find minimum bags require for packing. I want to use knapsack method for this.

You have to remove const as Shahroozevsky already mentioned. And a dictinary consists of key:value pairs. The key must be immutable. Which means you can use strings, numbers or tuples as dictionary keys.

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

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

A valid Python example should look something like this:

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'])

Output: cookie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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