Làm cách nào để lấy một giá trị từ từ điển trong Python?

Chúng tôi có rất nhiều biến thể và ứng dụng của bộ chứa từ điển trong Python và đôi khi, chúng tôi muốn thực hiện lọc các khóa trong từ điển, tôi. e chỉ trích xuất các khóa có trong vùng chứa cụ thể. Hãy thảo luận về những cách nhất định mà điều này có thể được thực hiện.  

Phương pháp 1. Trích xuất các khóa cụ thể từ từ điển bằng cách sử dụng hiểu từ điển + mục()

Vấn đề này có thể được thực hiện bằng cách xây dựng lại bằng cách sử dụng các khóa được trích xuất thông qua chức năng mục muốn được lọc và chức năng từ điển tạo từ điển mong muốn

Python3




The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
8

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
9
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
1
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
2
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
4
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
1
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
5
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
7
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
9
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
1
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
2

 

The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
3

The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
4
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
5
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
6
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
7
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
8
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
9

 

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
80

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
81

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
82
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
84
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
85
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
86
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
87
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
88

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
89
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
90
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
5
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
2
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
94

 

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
95

The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
4
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
5
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
98
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
7
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
8
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
01

đầu ra.  

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}

Phương pháp 2. Trích xuất các khóa cụ thể từ từ điển bằng dict()

Hàm dict() có thể được sử dụng để thực hiện tác vụ này bằng cách chuyển đổi logic được thực hiện bằng cách sử dụng khả năng hiểu danh sách thành một từ điển.  

Python3




The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
8

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
9
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
1
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
2
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
4
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
1
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
5
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
7
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
9
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
3
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
1
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
2

 

The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
3

The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
4
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
5
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
6
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
7
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
8
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
9

 

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
29

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
81

The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
82
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
0
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
33
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
5
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
45
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
5
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
47
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
48
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
49
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
42
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
87
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
52

Làm cách nào để in giá trị từ từ điển bằng Python?

Chính tả của Python. có thể sử dụng phương thức values() để truy xuất các giá trị từ điển, sau đó có thể in các giá trị này bằng cách sử dụng hàm print() .

Làm cách nào để truy cập một khóa trong từ điển Python?

Phương pháp #1. Sử dụng list() + keys() Có thể sử dụng kết hợp các phương pháp trên để thực hiện tác vụ cụ thể này. Trong phần này, chúng tôi chỉ chuyển đổi toàn bộ các khóa của từ điển được trích xuất bởi keys() thành một danh sách và chỉ cần truy cập vào khóa đầu tiên. Chỉ có một điều bạn phải ghi nhớ trong khi sử dụng tôi. e nó phức tạp.