Hướng dẫn get part of dictionary python - lấy một phần của python từ điển

Được rồi, đây là điều đã làm phiền tôi một vài lần, vì vậy cảm ơn bạn Jayesh đã hỏi nó.

Các câu trả lời ở trên có vẻ như là một giải pháp tốt như bất kỳ, nhưng nếu bạn đang sử dụng tất cả các mã của mình, thì thật hợp lý khi kết thúc chức năng IMHO. Ngoài ra, có hai trường hợp sử dụng có thể ở đây: một trong đó bạn quan tâm đến việc tất cả các từ khóa có trong từ điển gốc hay không. Và một nơi mà bạn không. Sẽ rất tốt nếu đối xử bình đẳng.

Vì vậy, đối với giá trị hai penneth của tôi, tôi khuyên bạn nên viết một lớp từ điển, ví dụ:

class my_dict[dict]:
    def subdict[self, keywords, fragile=False]:
        d = {}
        for k in keywords:
            try:
                d[k] = self[k]
            except KeyError:
                if fragile:
                    raise
        return d

Bây giờ bạn có thể rút ra một từ điển phụ với

orig_dict.subdict[keywords]

Ví dụ sử dụng:

#
## our keywords are letters of the alphabet
keywords = 'abcdefghijklmnopqrstuvwxyz'
#
## our dictionary maps letters to their index
d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
print['Original dictionary:\n%r\n\n' % [d,]]
#
## constructing a sub-dictionary with good keywords
oddkeywords = keywords[::2]
subd = d.subdict[oddkeywords]
print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
#
## constructing a sub-dictionary with mixture of good and bad keywords
somebadkeywords = keywords[1::2] + 'A'
try:
    subd2 = d.subdict[somebadkeywords]
    print["We shouldn't see this message"]
except KeyError:
    print["subd2 construction fails:"]
    print["\toriginal dictionary doesn't contain some keys\n\n"]
#
## Trying again with fragile set to false
try:
    subd3 = d.subdict[somebadkeywords, fragile=False]
    print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
except KeyError:
    print["We shouldn't see this message"]

Nếu bạn chạy tất cả các mã trên, bạn sẽ thấy [một cái gì đó như] đầu ra sau [xin lỗi cho định dạng]:

Từ điển gốc: {'a': 0, 'c': 2, 'b': 1, 'e': 4, 'd': 3, 'g': 6, 'f': 5, 'i': 8, 'H': 7, 'K': 10, 'J': 9, 'M': 12, 'L': 11, 'O': 14, 'N': 13, 'Q': 16, 'P': 15, 's': 18, 'r': 17, 'u': 20, 't': 19, 'w': 22, 'v': 21, 'y': 24, 'x ': 23,' z ': 25}
{'a': 0, 'c': 2, 'b': 1, 'e': 4, 'd': 3, 'g': 6, 'f': 5, 'i': 8, 'h': 7, 'k': 10, 'j': 9, 'm': 12, 'l': 11, 'o': 14, 'n': 13, 'q': 16, 'p': 15, 's': 18, 'r': 17, 'u': 20, 't': 19, 'w': 22, 'v': 21, 'y': 24, 'x': 23, 'z': 25}

Từ điển từ các khóa số lẻ được đánh số: {'a': 0, 'c': 2, 'e': 4, 'g': 6, 'i': 8, 'k': 10, 'm': 12, ' o ': 14,' q ': 16,' s ': 18,' u ': 20,' w ': 22,' y ': 24}
{'a': 0, 'c': 2, 'e': 4, 'g': 6, 'i': 8, 'k': 10, 'm': 12, 'o': 14, 'q': 16, 's': 18, 'u': 20, 'w': 22, 'y': 24}

Subd2 Construction Fails: Từ điển gốc không chứa một số phím
original dictionary doesn't contain some keys

Từ điển được xây dựng bằng một số khóa xấu: {'B': 1, 'd': 3, 'f': 5, 'h': 7, 'j': 9, 'l': 11, 'n': 13, 'P': 15, 'r': 17, 't': 19, 'v': 21, 'x': 23, 'z': 25}
{'b': 1, 'd': 3, 'f': 5, 'h': 7, 'j': 9, 'l': 11, 'n': 13, 'p': 15, 'r': 17, 't': 19, 'v': 21, 'x': 23, 'z': 25}

Xem thảo luận

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 = {'nikhil':

    orig_dict.subdict[keywords]
    0____11
    orig_dict.subdict[keywords]
    2:
    orig_dict.subdict[keywords]
    4
    orig_dict.subdict[keywords]
    1
    orig_dict.subdict[keywords]
    6:
    orig_dict.subdict[keywords]
    8__

    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}
    8
    orig_dict.subdict[keywords]
    6
    orig_dict.subdict[keywords]
    1'nikhil'
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    2

    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    4
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    5
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    6
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    7
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    8
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    9

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

    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    4
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    5
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    5
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    7
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    8
    The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
    The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
    8
     

    Đầ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 = {'nikhil':

    orig_dict.subdict[keywords]
    0____11
    orig_dict.subdict[keywords]
    2:
    orig_dict.subdict[keywords]
    4
    orig_dict.subdict[keywords]
    1
    orig_dict.subdict[keywords]
    6:
    orig_dict.subdict[keywords]
    8__

    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    4
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    5
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    6
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    7
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    8
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    9

    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    4
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    5
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    6
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    7
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    8
    #
    ## our keywords are letters of the alphabet
    keywords = 'abcdefghijklmnopqrstuvwxyz'
    #
    ## our dictionary maps letters to their index
    d = my_dict[[[k,i] for i,k in enumerate[keywords]]]
    print['Original dictionary:\n%r\n\n' % [d,]]
    #
    ## constructing a sub-dictionary with good keywords
    oddkeywords = keywords[::2]
    subd = d.subdict[oddkeywords]
    print['Dictionary from odd numbered keys:\n%r\n\n' % [subd,]]
    #
    ## constructing a sub-dictionary with mixture of good and bad keywords
    somebadkeywords = keywords[1::2] + 'A'
    try:
        subd2 = d.subdict[somebadkeywords]
        print["We shouldn't see this message"]
    except KeyError:
        print["subd2 construction fails:"]
        print["\toriginal dictionary doesn't contain some keys\n\n"]
    #
    ## Trying again with fragile set to false
    try:
        subd3 = d.subdict[somebadkeywords, fragile=False]
        print['Dictionary constructed using some bad keys:\n%r\n\n' % [subd3,]]
    except KeyError:
        print["We shouldn't see this message"]
    
    9

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

    Làm thế nào để bạn có được một tập hợp con của một từ điển?

    Sử dụng khả năng hiểu từ điển để lấy một tập hợp con của một từ điển. Sử dụng cú pháp {khóa: Giá trị cho khóa, giá trị trong dict. Các mục [] nếu điều kiện} Để tạo một từ điển mới chứa từng khóa: cặp giá trị trong dict cho điều kiện là đúng. DIGN.. Use the syntax {key: value for key, value in dict. items[] if condition} to create a new dictionary containing each key: value pair in dict for which condition is True . dict.

    Làm thế nào để bạn truy cập các yếu tố của một từ điển trong Python?

    Hãy thảo luận về các cách khác nhau để truy cập tất cả các khóa cùng với các giá trị của chúng trong từ điển Python ...
    Phương pháp số 1: Sử dụng toán tử ..
    Phương pháp số 2: Sử dụng danh sách hiểu ..
    Phương pháp số 3: Sử dụng dict.items [].
    Phương pháp số 4: Sử dụng Enumerate [].

    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 cắt một từ điển trong Python?

    Cho từ điển có giá trị như danh sách, cắt từng danh sách cho đến khi K. Input: test_dict = {Tiết kiệm là: [4, 6, 8]} Giải thích: Danh sách giá trị từ điển 3 chiều dài được trích xuất.slice each list till K. Input : test_dict = {“Gfg” : [1, 6, 3, 5, 7], “Best” : [5, 4, 2, 8, 9], “is” : [4, 6, 8, 4, 2]}, K = 3 Output : {'Gfg': [1, 6, 3], 'Best': [5, 4, 2], 'is': [4, 6, 8]} Explanation : The extracted 3 length dictionary value list.

    Bài Viết Liên Quan

    Chủ Đề