Đượ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 keysTừ đ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
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____11orig_dict.subdict[keywords]
2:
orig_dict.subdict[keywords]
4orig_dict.subdict[keywords]
1orig_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]
6orig_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"]
9The 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}
2The 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}
4The 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"]
5The 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"]
8The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}
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"]
#
## 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"]
#
## 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"]
#
## 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"]
Đầ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____11orig_dict.subdict[keywords]
2:
orig_dict.subdict[keywords]
4orig_dict.subdict[keywords]
1orig_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"]
9The 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}
2The 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}
4The 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}