- Bạn không thể sử dụng một bộ làm khóa của từ điển vì một bộ không thể thay đổi sau khi nó đã được tạo
- Bạn có thể kiểm tra sự tồn tại của một khóa trong từ điển bằng toán tử in
- Từ điển giống như một bộ dữ liệu để bạn không thể thay đổi từ điển sau khi nó đã được tạo
con trăn
Python là một ngôn ngữ lập trình cấp cao có mục đích chung. Python chủ yếu được sử dụng để tạo các ứng dụng web. Python sử dụng dấu thụt đầu dòng để hiển thị một khối mã
Trả lời và giải thích
- Bạn không thể sử dụng danh sách làm giá trị của từ điển vì danh sách có thể thay đổi bất cứ lúc nào
Sai Một danh sách là một nhóm các đối tượng có thứ tự và có thể thay đổi và nó cho phép trùng lặp. AS WELL AS Dictionary là một nhóm đối tượng có thể thay đổi và lập chỉ mục không theo thứ tự
- Bạn không thể sử dụng một bộ làm khóa của từ điển vì một bộ không thể thay đổi sau khi nó đã được tạo
Đúng vì tuple không thể thay đổi sau khi được tạo
- Bạn có thể
Xem câu trả lời đầy đủ bên dưới
Tìm hiểu thêm về chủ đề này
Nhận quyền truy cập vào video này và toàn bộ thư viện Hỏi & Đáp của chúng tôi
Ngôn ngữ lập trình. C ++, Java, Python và những thứ khác
từ
Chương 11 / Bài 15Một ngôn ngữ lập trình bao gồm một tập hợp các hướng dẫn hoặc mã mà máy tính có thể hiểu và thực thi. Tìm hiểu về các ngôn ngữ lập trình khác nhau, chẳng hạn như các ngôn ngữ lập trình đa năng như C, C++ và C#;
Từ điển Python [Tránh sai lầm]
Cải thiện bài viết
Lưu bài viết
Thích bài viết
- Độ khó. Cơ bản
- Cập nhật lần cuối. 06 tháng 7 năm 2021
Cải thiện bài viết
Lưu bài viết
dict trong python là gì?
Từ điển Python tương tự như bảng băm trong các ngôn ngữ như C++. Từ điển được sử dụng để tạo cặp khóa giá trị trong python. Thay cho khóa, có thể sử dụng Số chuỗi và Tuple, v.v. Ở vị trí của các giá trị có thể có bất cứ điều gì. Từ điển Python được biểu thị bằng dấu ngoặc nhọn. Một từ điển trống được đại diện bởi {}. Trong từ điển Python, khóa và giá trị được phân tách bằng '. ' và cặp giá trị khóa được phân tách bằng ', '. Ví dụ dưới đây giải thích rõ ràng
Thí dụ
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary
=
{}
Hello how are you thanks visit again0
mydictionary
=
Hello how are you thanks visit again3_______0_______4_______0_______5_______0_______6
Hello how are you thanks visit again7
Hello how are you thanks visit again8_______0_______9
Hello how are you thanks visit again5
Hello how are you thanks visit again None1_______0_______7
Hello how are you thanks visit again8_______9_______4_______0_______5
Hello how are you thanks visit again None6_______9_______7
Hello how are you thanks visit again None8
Hello how are you thanks visit again None9
Traceback [most recent call last]: File "/home/ce65dd34285f0cb0781de2a068e658fa.py", line 14, in print[mydictionary['college']] KeyError: 'college'0
đầu ra.
{'address': 'Allahabad', 'name': 'Ankit', 'college': 'MNNIT'}
được sử dụng ở đâu?
Từ điển cho chúng ta sức mạnh để mô hình hóa nhiều ứng dụng trong thế giới thực. Chúng tôi có thể tạo một từ điển về người nơi chúng tôi có thể lưu trữ tất cả thông tin liên quan đến người đó như tên tuổi địa điểm liên hệ, v.v. Trong từ điển có thể lưu trữ bất kỳ loại thông tin nào như từ và nghĩa của chúng. Từ điển Python được sử dụng rất nhiều trong học máy, nơi máy nói chuyện với con người trong tình huống đó, một số từ được xác định trước được lưu trữ dưới dạng khóa và ý nghĩa của chúng dưới dạng giá trị và khi người dùng muốn bất kỳ thứ gì được tìm kiếm trong khóa nếu tìm thấy thì giá trị của nó sẽ được trả về ngược lại . Trên thực tế, từ điển python có thể được sử dụng ở bất cứ đâu sử dụng hàm băm trong ngôn ngữ cũ thông thường.
Ví dụ để hiểu việc sử dụng dict
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary
=
{}
Hello how are you thanks visit again0
mydictionary
=
Hello how are you thanks visit again3
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}0_______0_______5
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}2
Hello how are you thanks visit again7
Hello how are you thanks visit again8_______22_______5
Hello how are you thanks visit again5
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}7
Hello how are you thanks visit again7
Hello how are you thanks visit again8_______96_______0_______0_______5
# program to understand dictionary in python
2Hello how are you thanks visit again None7
# program to understand dictionary in python
4
Hello how are you thanks visit again None9
# program to understand dictionary in python
6Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}0
# program to understand dictionary in python
8Hello how are you thanks visit again None9
# program to understand dictionary in python
6Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}5
# program to understand dictionary in python
8Hello how are you thanks visit again None9
# program to understand dictionary in python
6# program to understand dictionary in python
0# program to understand dictionary in python
8đầu ra.
Hello how are you thanks visit again
Những lỗi thường gặp khi sử dụng dicts và cách khắc phục
Sau đây là một số lỗi khi sử dụng dict trong python.
1] Để truy cập phần tử của từ điển trong python, không bao giờ truy cập trực tiếp phần tử bằng tên khóa, luôn cố gắng sử dụng. lấy phương thức. Nếu chìa khóa không có mặt thì. phương thức get sẽ không in gì trong khi [key] sẽ chấm dứt toàn bộ chương trình.
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary
=
{}
Hello how are you thanks visit again0
mydictionary
=
Hello how are you thanks visit again3
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}0_______0_______5
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}2
Hello how are you thanks visit again7
Hello how are you thanks visit again8_______22_______5
Hello how are you thanks visit again5
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}7
Hello how are you thanks visit again7
Hello how are you thanks visit again8_______96_______0_______0_______5
# program to understand dictionary in python
2Hello how are you thanks visit again None7
# program to understand dictionary in python
4
Hello how are you thanks visit again None9
# program to understand dictionary in python
6Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}0
# program to understand dictionary in python
8Hello how are you thanks visit again None9
# program to understand dictionary in python
6Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}5
# program to understand dictionary in python
8Hello how are you thanks visit again None9
# program to understand dictionary in python
6# program to understand dictionary in python
0# program to understand dictionary in python
8
Hello how are you thanks visit again03
Hello how are you thanks visit again None9
Hello how are you thanks visit again05_______0_______9
Hello how are you thanks visit again07
Hello how are you thanks visit again08
Hello how are you thanks visit again None9
# program to understand dictionary in python
6Hello how are you thanks visit again9
# program to understand dictionary in python
8đầu ra.
Hello how are you thanks visit again None
Lỗi runtime
Traceback [most recent call last]: File "/home/ce65dd34285f0cb0781de2a068e658fa.py", line 14, in print[mydictionary['college']] KeyError: 'college'
2] Khi muốn sao chép từ điển này sang từ điển khác thì cần có kiến thức đúng về phương pháp sao chép
- new_dictionary = old_dictionary. Dòng này có nghĩa là old_dictionary và new_dictionary sẽ đề cập đến cùng một đối tượng có nghĩa là thay đổi trong một từ điển sẽ phản ánh sang từ điển khác
- new_dictionary = dict[old_dictionary] và new_dictionary = old_dictionary. sao chép[]. sẽ sao chép từ điển cũ sang từ điển mới có nghĩa là cập nhật trong cũ sẽ không phản ánh cập nhật trong d nhưng các giá trị trong e sẽ được sao chép bằng cách sử dụng tham chiếu. Điều này sẽ thực hiện sao chép nông
- new_dictionary = bản sao. bản sao sâu [old_dictionary]. Điều này sẽ tạo ra một bản sao sâu
Python3
# program to understand dictionary in python
Hello how are you thanks visit again14
# creating an empty dictionary
mydictionary
=
{}
Hello how are you thanks visit again14
Hello how are you thanks visit again0
mydictionary
=
Hello how are you thanks visit again3
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}0_______0_______5
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}2
Hello how are you thanks visit again7
Hello how are you thanks visit again8_______22_______5
Hello how are you thanks visit again5
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}7
Hello how are you thanks visit again7
Hello how are you thanks visit again8_______96_______0_______0_______5
# program to understand dictionary in python
2Hello how are you thanks visit again None7
Hello how are you thanks visit again14
# program to understand dictionary in python
4
Hello how are you thanks visit again None9
# program to understand dictionary in python
6Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}0
# program to understand dictionary in python
8Hello how are you thanks visit again None9
# program to understand dictionary in python
6Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}5
# program to understand dictionary in python
8Hello how are you thanks visit again None9
# program to understand dictionary in python
6# program to understand dictionary in python
0# program to understand dictionary in python
8
Hello how are you thanks visit again52
Hello how are you thanks visit again53____99_______
mydictionary
Hello how are you thanks visit again None9
Hello how are you thanks visit again57
đầu ra.
Hello how are you thanks visit again {'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}
Khi nào không sử dụng dicts?
Python dict rất hữu ích trong nhiều tình huống nhưng trong một số tình huống phải tránh sử dụng chúng. Trong python đừng bao giờ nghĩ rằng chỉ có từ điển là mảng kết hợp. Trong dict, chúng ta nên cố gắng lưu trữ các giá trị cùng loại.
- Nếu chúng ta muốn tìm kiếm các giá trị xem các giá trị có trong từ điển hay không luôn sử dụng trong tình huống đó python set vì trong python set là một mảng kết hợp với các giá trị bool cho dù phần tử có hay không
- Đối với số thuộc tính cố định, luôn sử dụng Lớp hoặc Tuple có tên trong python
- Khi bạn muốn một tin nhắn được xác định trước khi một khóa cụ thể thì hãy sử dụng các bộ sưu tập. mặc định trong python
Ghi chú cá nhân của tôi arrow_drop_up
Tiết kiệm