Trong Python, từ điển là một trong những cấu trúc dữ liệu cốt lõi. Nó là một chuỗi các cặp khóa-giá trị được phân tách bằng dấu phẩy và được bao quanh bởi dấu ngoặc nhọn
Nếu bạn đã quen thuộc với JavaScript, từ điển Python giống như các đối tượng JavaScript
Python cung cấp hơn 10 phương thức để làm việc với từ điển
Trong bài viết này, tôi sẽ chỉ cho bạn cách tạo một từ điển trong Python và làm việc với nó bằng các phương thức đó
Những gì chúng tôi sẽ bao gồm
Cách tạo từ điển bằng Python
Để tạo từ điển, bạn mở một dấu ngoặc nhọn và đặt dữ liệu vào cặp khóa-giá trị được phân tách bằng dấu phẩy
Cú pháp cơ bản của một từ điển trông như thế này
demo_dict = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Lưu ý rằng các giá trị có thể thuộc bất kỳ loại dữ liệu nào và có thể được sao chép, nhưng không được sao chép khóa. Nếu các khóa bị trùng lặp, bạn sẽ gặp lỗi cú pháp không hợp lệ
Phương pháp làm việc với từ điển Python
Tôi sẽ làm việc với từ điển bên dưới để chỉ cho bạn cách thức hoạt động của các phương thức từ điển
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
Cách Sử dụng Phương pháp Từ điển first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
1
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
Phương thức get trả về giá trị của một khóa đã chỉ định
Trong đoạn mã dưới đây, tôi có thể lấy được người sáng lập freeCodeCamp bằng cách chuyển khóa
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
founder = first_dict.get["founder"]
print[founder]
# Output: Quincy Larson
1 bên trong phương thức first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
1first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
founder = first_dict.get["founder"]
print[founder]
# Output: Quincy Larson
Cách Sử dụng Phương pháp Từ điển first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
2
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
Phương thức
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
2 trả về tất cả các mục của từ điển trong một danh sách. Trong danh sách là một tuple đại diện cho từng mục________số 8Cách Sử dụng Phương pháp Từ điển first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
3
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
3 trả về tất cả các khóa trong từ điển. Nó trả về các khóa trong một bộ – một cấu trúc dữ liệu khác của Pythonfirst_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
1Cách Sử dụng Phương pháp Từ điển first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
4
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
Phương thức giá trị truy cập tất cả các giá trị trong từ điển. Giống như phương thức
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
3, nó trả về các giá trị trong một bộfirst_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
4Cách Sử dụng Phương pháp Từ điển first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
5
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
Phương thức
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
5 xóa một cặp khóa-giá trị khỏi từ điển. Để làm cho nó hoạt động, bạn cần chỉ định khóa bên trong dấu ngoặc đơn của nófirst_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
7Bạn có thể thấy khóa
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
items = first_dict.items[]
print[items]
# Output: dict_items[[['name', 'freeCodeCamp'], ['founder', 'Quincy Larson'], ['type', 'charity'], ['age', 8], ['price', 'free'], ['work-style', 'remote']]]
1 và giá trị của nó đã bị xóa khỏi từ điểnCách Sử dụng Phương pháp Từ điển first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
6
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
Phương pháp
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
6 hoạt động giống như phương pháp first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
5. Sự khác biệt là nó loại bỏ mục cuối cùng trong từ điểnfirst_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
2Bạn có thể thấy rằng cặp khóa-giá trị cuối cùng ["kiểu công việc". "từ xa"] đã bị xóa khỏi từ điển
Cách Sử dụng Phương pháp Từ điển first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
7
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
Phương thức
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
7 thêm một mục vào từ điển. Bạn phải chỉ định cả khóa và giá trị bên trong dấu ngoặc nhọn của nó và bao quanh nó bằng dấu ngoặc nhọnfirst_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
5Mục mới đã được thêm vào từ điển
Cách Sử dụng Phương pháp Từ điển first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
8
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
Phương thức
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
8 thực hiện đúng như tên gọi của nó – nó sao chép từ điển vào biến được chỉ địnhfirst_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
8Cách Sử dụng Phương pháp Từ điển first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
9
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
Phương thức rõ ràng loại bỏ tất cả các mục trong từ điển
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
0Phần kết luận
Trong bài viết này, bạn đã học cách tạo một từ điển Python và cách làm việc với nó bằng các phương thức tích hợp do Python cung cấp
Nếu thấy bài viết hữu ích đừng ngần ngại chia sẻ với bạn bè và người thân
Tiếp tục mã hóa. ]
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
QUẢNG CÁO
Nhà phát triển web và nhà văn kỹ thuật tập trung vào các công nghệ giao diện người dùng
Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn
Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu