Chúng ta có thể lồng nhau trong python không?

Bộ lồng nhau hay còn gọi là cây SQL Traversal Pre-order Tree Travers [MPTT] đã sửa đổi được triển khai bằng Python cho MySQL và SQLite. Sử dụng cả đánh dấu ngang [trái, phải] và danh sách phụ huynhId để linh hoạt hơn cho truy vấn đặc biệt

Cung cấp xác thực cấu trúc cây và “ghi nhớ” thông qua SQLite. kỉ niệm. để đọc nhanh

Thí dụ

Chúng tôi muốn đạt được cây sau. Nút được đại diện bởi tên id → parentId [l, r]. Để xuất một cây theo cách này fangorn. kiểm tra. chức năng trực quan hóa có thể được sử dụng

R 1 → None [1, 18]
└─A1 2 → 1 [2, 5]
  └─B1 3 → 2 [3, 4]
└─A2 4 → 1 [6, 13]
  └─B2 5 → 4 [7, 8]
  └─B3 6 → 4 [9, 12]
    └─C1 7 → 6 [10, 11]
└─A3 8 → 1 [14, 17]
  └─B4 9 → 8 [15, 16]

Đầu tiên chúng ta cần một bảng để đại diện cho cây. Và chúng tôi muốn một nút cây có tên

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']

Bây giờ chúng ta có thể tạo ví dụ cây. Lưu ý rằng DAO mà cây dựa vào dự kiến ​​sẽ hỗ trợ kiểu tham số DB-API có tên [i. e. NƠI node_id =. nútId]. Ngoài ra, các phương pháp kiểm soát giao dịch được khuyến nghị để triển khai hỗ trợ giao dịch lồng nhau. Tuy nhiên, bộ thử nghiệm yêu cầu các giao dịch lồng nhau để chạy. Đối với MySQLdb và sqlite3, có các trình bao bọc tương thích dưới fangorn. tương thích

Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
28
Ali
25
5
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Ali
25
7
Ali
25
8_______29
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Initial nested dictionary:-
{'Dict2': {'name': 'Bob', 'age': 21}, 'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict2:-
{'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict1:-
{}
1
Ali
25
8
Initial nested dictionary:-
{'Dict2': {'name': 'Bob', 'age': 21}, 'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict2:-
{'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict1:-
{}
3
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
29
Initial nested dictionary:-
{'Dict2': {'name': 'Bob', 'age': 21}, 'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict2:-
{'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict1:-
{}
5
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Initial nested dictionary:-
{'Dict2': {'name': 'Bob', 'age': 21}, 'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict2:-
{'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict1:-
{}
7
Ali
25
8
Initial nested dictionary:-
{'Dict2': {'name': 'Bob', 'age': 21}, 'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict2:-
{'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict1:-
{}
9
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
301
Ali
25
8
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
303
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Ali
25
7
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
9

Tạo một từ điển lồng nhau

Trong Python, từ điển lồng nhau có thể được tạo bằng cách đặt các từ điển được phân tách bằng dấu phẩy trong dấu ngoặc nhọn.
 

Python3




import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
307

Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
28
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
271
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
272

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
273
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
274
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
275

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
277____25
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
279
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
280

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
282
Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
284

 

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
285

Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
28
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
271
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
29
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
521
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
523
Ali
25
8
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
525
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
527
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
2

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
273
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
274
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
29
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
521
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
294
Ali
25
8
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
525
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
298
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
9

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
641
Ali
25
9
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
279
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
280

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
282
Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
284

 

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
649

Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
28
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
271
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
29
Ali
25
5
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0_______407
Ali
25
8
Ali
25
9
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
11
Ali
25
8
Initial nested dictionary:-
{'Dict2': {'name': 'Bob', 'age': 21}, 'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict2:-
{'Dict1': {'name': 'Ali', 'age': 19}}

Deleting Dict1:-
{}
3
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
07
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
2

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
273
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
274
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
29
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
20
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Ali
25
7
Ali
25
8
Ali
25
5
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
25
Ali
25
5
Ali
25
8
Ali
25
9
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
29

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
641____93
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
279
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
280

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
282
Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
284

Đầu ra.
 

Ali
25
1

 

Thêm các phần tử vào Từ điển lồng nhau

Việc thêm các phần tử vào Từ điển lồng nhau có thể được thực hiện theo nhiều cách. Một cách để thêm từ điển trong Nested dictionary là thêm các giá trị từng cái một, Nested_dict[dict][key] = ‘value’. Một cách khác là thêm toàn bộ từ điển trong một lần, Nested_dict[dict] = { ‘key’. 'giá trị'}.
 

Python3




Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
41

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
43
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
279
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
280

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
282
Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
284

 

Ali
25
2______451____1271
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
53
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
55

 

Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
56

Ali
25
2______451____1271
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
60
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
521
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
53
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
294

Ali
25
2______451____1271
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
60
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
525
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
53
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
72

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
74

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
282
Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
284

 

Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
79

Ali
25
2______451______1274
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
53
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
28
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
521
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
88
Ali
25
8
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
525
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
92
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
93

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
74

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
282
Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
284

Đầu ra.
 

Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}

 

Truy cập các phần tử của Từ điển lồng nhau

Để truy cập giá trị của bất kỳ khóa nào trong từ điển lồng nhau, hãy sử dụng cú pháp lập chỉ mục [].
 

Python3




import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
285

Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
28
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
271
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
29
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
521
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
523
Ali
25
8
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
525
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
527
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
2

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
273
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
274
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
29
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
521
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
294
Ali
25
8
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
525
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
298
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
9

 

Ali
25
25

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
282
Ali
25
2____451
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
271
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
60
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
521
Ali
25
33

 

Ali
25
34

import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
276
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
282
Ali
25
2
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
51
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
274
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
60
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
525
Ali
25
33

Đầu ra.
 

Ali
25

 

Xóa từ điển khỏi Từ điển lồng nhau

Việc xóa từ điển khỏi từ điển lồng nhau có thể được thực hiện bằng cách sử dụng từ khóa del hoặc bằng cách sử dụng hàm pop[].
 

Python3




Ali
25
2
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
27
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
28
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
271
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
29
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
521
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
523
Ali
25
8
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
525
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Ali
25
54
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
2

Ali
25
56
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
274
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
29
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
521
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
294
Ali
25
8
import MySQLdb as mysql
conn = mysql.connect[user = 'guest', db = 'test']
conn.query['''
  CREATE TABLE `node` [
    `node_id` int[10] unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int[10] unsigned DEFAULT NULL,
    `l` int[10] unsigned NOT NULL,
    `r` int[10] unsigned NOT NULL,
    `name` varchar[8] NOT NULL,
    PRIMARY KEY [`node_id`],
    KEY `l` [`l`],
    KEY `r` [`r`],
    KEY `parent_id` [`parent_id`],
    CONSTRAINT `node_has_node` FOREIGN KEY [`parent_id`]
      REFERENCES `node` [`node_id`]
      ON DELETE CASCADE
      ON UPDATE CASCADE
  ] ENGINE=InnoDB;
''']
525
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
0
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
72
Initial nested dictionary:-
{}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}

After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
9

Một tập hợp có thể có nhiều loại trong Python không?

Xác định Tập hợp . Bản thân một tập hợp có thể được sửa đổi, nhưng các phần tử chứa trong tập hợp phải thuộc loại bất biến. Duplicate elements are not allowed. A set itself may be modified, but the elements contained in the set must be of an immutable type.

Chúng ta có thể tạo tập hợp các tập hợp trong Python không?

Tạo bộ Python . Nó có thể có bất kỳ số lượng mục nào và chúng có thể thuộc các loại khác nhau [số nguyên, số float, bộ, chuỗi, v.v. ]. A set is created by placing all the items [elements] inside curly braces {} , separated by comma, or by using the built-in set[] function. It can have any number of items and they may be of different types [integer, float, tuple, string etc.].

Tôi có thể sử dụng cái gì thay vì set trong Python?

Python cho phép bạn giải quyết vấn đề này bằng cách sử dụng a freezeset để thay thế. Điều này có tất cả các thuộc tính của một tập hợp, ngoại trừ việc nó không thay đổi [điều này có nghĩa là bạn không thể thêm/xóa các phần tử khỏi tập hợp cố định].

Bạn có thể có một bộ danh sách bằng Python không?

Trong bất kỳ ngôn ngữ lập trình nào, danh sách được sử dụng để lưu trữ nhiều mục trong một biến. Trong Python, chúng ta có thể tạo danh sách bằng cách bao quanh tất cả các phần tử bằng dấu ngoặc vuông [] và mỗi phần tử được phân tách bằng dấu phẩy . Nó có thể được sử dụng để lưu trữ số nguyên, số float, chuỗi, v.v.

Chủ Đề