Hướng dẫn how do i extract a dictionary key in python? - làm cách nào để trích xuất khóa từ điển trong python?

Xem thảo luận

Show

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

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

    Python3

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

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

    test_dict

    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}
    5
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    6
    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}
    8
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    5__

    [1, 2, 3]
    1
    [1, 2, 3]
    2
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    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}
    2
    [1, 2, 3]
    6

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    8
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    9
    dict_keys([1, 2, 3])
    0
    dict_keys([1, 2, 3])
    1
    dict_keys([1, 2, 3])
    2
    dict_keys([1, 2, 3])
    3

    dict_keys([1, 2, 3])
    4
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    0
    dict_keys([1, 2, 3])
    6
    dict_keys([1, 2, 3])
    7
    dict_keys([1, 2, 3])
    8
    dict_keys([1, 2, 3])
    9
    [1, 2, 3]
    0

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

    Input : {1:'a', 2:'b', 3:'c'} Output : [1, 2, 3] Input : {'A' : 'ant', 'B' : 'ball'} Output : ['A', 'B']8Input : {1:'a', 2:'b', 3:'c'} Output : [1, 2, 3] Input : {'A' : 'ant', 'B' : 'ball'} Output : ['A', 'B']9[1, 2, 3]9 dict_keys([1, 2, 3])1 dict_keys([1, 2, 3])2[1, 2, 3]2 

    Đầu ra: & nbsp;

    Python3

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

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

    test_dict

    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}
    5
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    6
    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}
    8
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    5__

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    8
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    9
    dict_keys([1, 2, 3])
    0
    dict_keys([1, 2, 3])
    1
    dict_keys([1, 2, 3])
    2
    dict_keys([1, 2, 3])
    3

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    8
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    9
    dict_keys([1, 2, 3])
    0
    dict_keys([1, 2, 3])
    1
    dict_keys([1, 2, 3])
    2
    dict_keys([1, 2, 3])
    3

    dict_keys([1, 2, 3])
    4
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    0
    dict_keys([1, 2, 3])
    6
    dict_keys([1, 2, 3])
    7
    dict_keys([1, 2, 3])
    8
    dict_keys([1, 2, 3])
    9
    [1, 2, 3]
    0

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

    Đưa ra một từ điển, hãy viết một chương trình Python để lấy các khóa từ điển làm danh sách. & NBSP;

    Examples:

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']

    Phương thức & NBSP; 1: Nhận các khóa từ điển làm danh sách bằng Dict.Keys () Get dictionary keys as a list using dict.keys()

    Phương thức Dict.Keys () trong Từ điển Python, trả về một đối tượng Xem hiển thị danh sách tất cả các khóa trong từ điển theo thứ tự chèn.

    Python3

    Is

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    8
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    9
    ['a', 'b', 'c']
    0
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    19

    Output:

    dict_keys([1, 2, 3])

    Phương pháp 2: Nhận các khóa từ điển làm danh sách bằng Dict.Keys () Get dictionary keys as a list using dict.keys()

    Hàm danh sách Python () lấy bất kỳ tham số nào và trả về danh sách. Trong Python Itable là đối tượng bạn có thể lặp lại.

    Python3

    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    20
    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}
    4
    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}
    06
    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}
    8
    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}
    10
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    5
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    222

    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    35
    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}
    37
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    38

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    8
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    40

    Output:

    [1, 2, 3]

    Phương thức & NBSP; 3: Nhận các khóa từ điển làm danh sách sử dụng cho phương thức vòng lặp và nối Get dictionary keys as a list using For Loop and append method

    Trong phương thức này, chúng tôi sẽ lặp lại từng khóa bằng cách sử dụng hàm Dict.Keys () và nối chúng vào một danh sách mới có tên là Danh sách.

    Python3

    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    41
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    42
    ['a', 'b', 'c']
    0____
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    44

    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    45
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    59
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    37
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    9
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    46
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    47
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    48
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    49
    ['a', 'b', 'c']
    0____
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    51

    Is

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

    Làm thế nào để bạn có được một khóa từ một từ điển?

    Vì câu hỏi giả định từ điển chỉ có một cặp giá trị khóa, tôi muốn thêm hai phương thức nữa bên cạnh câu trả lời được chấp nhận ..

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    8
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    77
    ['a', 'b', 'c']
    0
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    79

    Output:

    [1, 2, 3]

    Sử dụng dict. Popitem () [0]. PopItem () trả về cặp giá trị khóa duy nhất trong tuple: (khóa, giá trị). ....

    Xây dựng một tập hợp và sau đó pop: set (mydict). nhạc pop() ..

    Python3

    Is

    Phương pháp 2: Nhận các khóa từ điển làm danh sách bằng Dict.Keys ()

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    8
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    40

    Output:

    [1, 2, 3]

    Hàm danh sách Python () lấy bất kỳ tham số nào và trả về danh sách. Trong Python Itable là đối tượng bạn có thể lặp lại. Dictionary Keys to List using Unpacking with * 

    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    20
    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}
    4
    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}
    06
    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}
    8
    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}
    10
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    5
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    222

    Python3

    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    41
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    42
    ['a', 'b', 'c']
    0____
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    44

    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    35
    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}
    37
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    38

    Is

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    8
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    77
    ['a', 'b', 'c']
    0
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    79

    Output:

    ['a', 'b', 'c']

    Phương thức & NBSP; 6: Khóa từ điển để liệt kê bằng cách sử dụng Itemgetter Dictionary Keys to List using itemgetter

    Itemgetter từ mô -đun toán tử trả về một đối tượng có thể gọi được để lấy một mục từ toán hạng của nó bằng phương thức toán hạng __getitem __ (). Phương thức này sau đó được ánh xạ tới Dict.Items () và sau đó được đánh máy vào Liệt kê. & NBSP;

    Python3

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    34
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    35
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    36
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    37

    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    41
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    42
    ['a', 'b', 'c']
    0____
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    44

    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    45
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    59
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    37
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    9
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    46
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    47
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    48
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    49
    ['a', 'b', 'c']
    0____
    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    51

    Is

    Input  : {1:'a', 2:'b', 3:'c'}
    Output : [1, 2, 3]
    
    Input  : {'A' : 'ant', 'B' : 'ball'}
    Output : ['A', 'B']
    8
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    77
    ['a', 'b', 'c']
    0
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    79

    Output:

    ['a', 'b', 'c']

    Làm thế nào để bạn có được một khóa từ một từ điển?

    Vì câu hỏi giả định từ điển chỉ có một cặp giá trị khóa, tôi muốn thêm hai phương thức nữa bên cạnh câu trả lời được chấp nhận ...
    Sử dụng dict. Popitem () [0]. PopItem () trả về cặp giá trị khóa duy nhất trong tuple: (khóa, giá trị). ....
    Xây dựng một tập hợp và sau đó pop: set (mydict). nhạc pop() ..

    Làm cách nào để trích xuất dữ liệu từ một từ điển trong Python?

    Dưới đây là 3 cách tiếp cận để trích xuất các giá trị từ điển như một danh sách trong Python:..
    .
    .
    .

    Làm thế nào để bạn tách một khóa khỏi một giá trị trong từ điển?

    Tạo một từ điển để làm điều đó bạn tách các cặp giá trị khóa bằng một dấu hai chấm (Hồi: Hồi).Các khóa sẽ cần phải thuộc loại bất biến, tức là, các loại dữ liệu mà các phím không thể thay đổi trong thời gian chạy như int, chuỗi, tuple, v.v. Các giá trị có thể thuộc bất kỳ loại nào.by a colon(“:”). The keys would need to be of an immutable type, i.e., data-types for which the keys cannot be changed at runtime such as int, string, tuple, etc. The values can be of any type.

    Làm thế nào để bạn tìm nạp và chỉ hiển thị các khóa của một từ điển trong Python?

    Phương thức từ điển Python Phương thức Phương thức Phương thức () Phương thức trong Từ điển Python, trả về một đối tượng Chế độ xem hiển thị danh sách tất cả các khóa trong từ điển theo thứ tự chèn bằng Python.Tham số: Không có tham số.Trả về: Một đối tượng xem được trả về hiển thị tất cả các khóa. The keys() method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion using Python. Parameters: There are no parameters. Returns: A view object is returned that displays all the keys.