पायथन डिक्शनरी (उदाहरणों के साथ)

इस ट्यूटोरियल में, आप पायथन शब्दकोशों के बारे में सब कुछ जानेंगे; वे कैसे बनाए जाते हैं, उन तक तत्वों को पहुँचाना, जोड़ना, हटाना और विभिन्न अंतर्निहित विधियाँ।

वीडियो: कुंजी / मूल्य जोड़े स्टोर करने के लिए पायथन डिक्शनरी

पायथन शब्दकोश वस्तुओं का एक अनियंत्रित संग्रह है। शब्दकोश के प्रत्येक आइटम में एक key/valueजोड़ी है।

कुंजी ज्ञात होने पर मान पुनः प्राप्त करने के लिए शब्दकोशों को अनुकूलित किया जाता है।

पायथन डिक्शनरी बनाना

एक शब्दकोश बनाना उतना ही सरल है जितना ()कि कॉमा द्वारा अलग किए गए घुंघराले ब्रेस के अंदर आइटम रखना।

एक आइटम में एक keyऔर एक है valueजो एक जोड़ी ( कुंजी: मान ) के रूप में व्यक्त किया गया है ।

हालांकि मान किसी भी डेटा प्रकार के हो सकते हैं और दोहरा सकते हैं, कुंजियाँ अपरिवर्तनीय प्रकार (स्ट्रिंग, संख्या या अपरिवर्तनीय तत्वों के साथ ट्यूपल) की होनी चाहिए और अद्वितीय होनी चाहिए।

 # empty dictionary my_dict = () # dictionary with integer keys my_dict = (1: 'apple', 2: 'ball') # dictionary with mixed keys my_dict = ('name': 'John', 1: (2, 4, 3)) # using dict() my_dict = dict((1:'apple', 2:'ball')) # from sequence having each item as a pair my_dict = dict(((1,'apple'), (2,'ball')))

जैसा कि आप ऊपर से देख सकते हैं, हम अंतर्निहित dict()फ़ंक्शन का उपयोग करके एक शब्दकोश भी बना सकते हैं ।

शब्दकोश से तत्वों तक पहुँचने

मूल्यों को एक्सेस करने के लिए अन्य डेटा प्रकारों के साथ इंडेक्सिंग का उपयोग किया जाता है keys। कुंजी का उपयोग या तो चौकोर कोष्ठक के अंदर ()या get()विधि के साथ किया जा सकता है ।

यदि हम चौकोर कोष्ठक का उपयोग करते हैं (), KeyErrorतो शब्दकोश में एक कुंजी नहीं पाए जाने पर उठाया जाता है। दूसरी ओर, कुंजी नहीं get()मिलने पर विधि वापस आ Noneजाती है।

 # get vs () for retrieving elements my_dict = ('name': 'Jack', 'age': 26) # Output: Jack print(my_dict('name')) # Output: 26 print(my_dict.get('age')) # Trying to access keys which doesn't exist throws error # Output None print(my_dict.get('address')) # KeyError print(my_dict('address'))

आउटपुट

 जैक 26 कोई नहीं ट्रेसबैक (सबसे हालिया कॉल अंतिम): फाइल "", लाइन 15, प्रिंट में (my_dict ('पता')) KeyError: 'पता'

शब्दकोश तत्वों को बदलना और जोड़ना

शब्दकोश परस्पर हैं। हम नए आइटम जोड़ सकते हैं या असाइनमेंट ऑपरेटर का उपयोग करके मौजूदा वस्तुओं के मूल्य को बदल सकते हैं।

यदि कुंजी पहले से मौजूद है, तो मौजूदा मान अपडेट हो जाता है। यदि कुंजी मौजूद नहीं है, तो शब्दकोश में एक नई ( कुंजी: मान ) जोड़ी जोड़ी जाती है।

 # Changing and adding Dictionary Elements my_dict = ('name': 'Jack', 'age': 26) # update value my_dict('age') = 27 #Output: ('age': 27, 'name': 'Jack') print(my_dict) # add item my_dict('address') = 'Downtown' # Output: ('address': 'Downtown', 'age': 27, 'name': 'Jack') print(my_dict)

आउटपुट

 ('नाम': 'जैक', 'आयु': 27) ('नाम': 'जैक', 'आयु': 27, 'एड्रेस': 'डाउनटाउन')

शब्दकोश से तत्वों को हटाना

हम pop()विधि का उपयोग करके किसी विशेष आइटम को शब्दकोश में निकाल सकते हैं । यह विधि प्रदान किए गए आइटम को हटा देती है और आइटम को keyलौटा देती है value

popitem()विधि निकालकर एक मनमाना वापस जाने के लिए इस्तेमाल किया जा सकता (key, value)शब्दकोश से आइटम जोड़ी। clear()विधि का उपयोग करके सभी वस्तुओं को एक बार में हटाया जा सकता है ।

हम delव्यक्तिगत आइटम या संपूर्ण शब्दकोश को हटाने के लिए भी कीवर्ड का उपयोग कर सकते हैं ।

 # Removing elements from a dictionary # create a dictionary squares = (1: 1, 2: 4, 3: 9, 4: 16, 5: 25) # remove a particular item, returns its value # Output: 16 print(squares.pop(4)) # Output: (1: 1, 2: 4, 3: 9, 5: 25) print(squares) # remove an arbitrary item, return (key,value) # Output: (5, 25) print(squares.popitem()) # Output: (1: 1, 2: 4, 3: 9) print(squares) # remove all items squares.clear() # Output: () print(squares) # delete the dictionary itself del squares # Throws Error print(squares)

आउटपुट

 16 (1: 1, 2: 4, 3: 9, 5: 25) (5, 25) (1: 1, 2: 4, 3: 9) () ट्रेसबैक (सबसे हालिया कॉल अंतिम): फ़ाइल "", पंक्ति 30, प्रिंट में (वर्ग) NameError: नाम 'वर्ग' परिभाषित नहीं है

पायथन डिक्शनरी मेथड्स

एक शब्दकोश के साथ उपलब्ध तरीके नीचे सारणीबद्ध हैं। उनमें से कुछ पहले से ही उपरोक्त उदाहरणों में उपयोग किए गए हैं।

तरीका विवरण
स्पष्ट() शब्दकोश से सभी आइटम निकालता है।
कॉपी () शब्दकोश की एक उथली प्रति लौटाता है।
सेकी (seq (, v)) Seq और v के बराबर मान (चूक None) से कुंजियों के साथ एक नया शब्दकोश देता है ।
मिल (कुंजी, (डी)) कुंजी का मान लौटाता है। यदि कुंजी मौजूद नहीं है, तो d (डिफ़ॉल्‍ट टू None) लौटाता है ।
आइटम () शब्दकोश के आइटम (कुंजी, मूल्य) प्रारूप में एक नई वस्तु लौटाएं।
चांबियाँ() शब्दकोश की कुंजियों की एक नई वस्तु लौटाता है।
पॉप (कुंजी, (डी)) कुंजी के साथ आइटम को निकालता है और कुंजी नहीं मिलने पर उसका मूल्य या डी वापस करता है। यदि d प्रदान नहीं किया गया है और कुंजी नहीं मिली है, तो यह बढ़ जाता है KeyError
पॉपिटेम () एक मनमाना आइटम ( कुंजी, मान ) निकालता है और लौटाता है । KeyErrorशब्दकोश खाली होने पर उठाता है।
सेटडफ़ॉल्ट (कुंजी, डी) यदि शब्दकोश में कुंजी है तो संबंधित मान लौटाता है। यदि नहीं, तो कुंजी को d के मान के साथ सम्मिलित करता है और d (रिटर्न डिफॉल्ट None) करता है।
अद्यतन ((अन्य)) Updates the dictionary with the key/value pairs from other, overwriting existing keys.
values() Returns a new object of the dictionary's values

Here are a few example use cases of these methods.

 # Dictionary Methods marks = ().fromkeys(('Math', 'English', 'Science'), 0) # Output: ('English': 0, 'Math': 0, 'Science': 0) print(marks) for item in marks.items(): print(item) # Output: ('English', 'Math', 'Science') print(list(sorted(marks.keys())))

Output

 ('Math': 0, 'English': 0, 'Science': 0) ('Math', 0) ('English', 0) ('Science', 0) ('English', 'Math', 'Science')

Python Dictionary Comprehension

Dictionary comprehension is an elegant and concise way to create a new dictionary from an iterable in Python.

Dictionary comprehension consists of an expression pair (key: value) followed by a for statement inside curly braces ().

Here is an example to make a dictionary with each item being a pair of a number and its square.

 # Dictionary Comprehension squares = (x: x*x for x in range(6)) print(squares)

Output

 (0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25)

This code is equivalent to

 squares = () for x in range(6): squares(x) = x*x print(squares)

Output

 (0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25)

A dictionary comprehension can optionally contain more for or if statements.

An optional if statement can filter out items to form the new dictionary.

Here are some examples to make a dictionary with only odd items.

 # Dictionary Comprehension with if conditional odd_squares = (x: x*x for x in range(11) if x % 2 == 1) print(odd_squares)

Output

 (1: 1, 3: 9, 5: 25, 7: 49, 9: 81)

To learn more dictionary comprehensions, visit Python Dictionary Comprehension.

Other Dictionary Operations

Dictionary Membership Test

We can test if a key is in a dictionary or not using the keyword in. Notice that the membership test is only for the keys and not for the values.

 # Membership Test for Dictionary Keys squares = (1: 1, 3: 9, 5: 25, 7: 49, 9: 81) # Output: True print(1 in squares) # Output: True print(2 not in squares) # membership tests for key only not value # Output: False print(49 in squares)

Output

 True True False

Iterating Through a Dictionary

We can iterate through each key in a dictionary using a for loop.

 # Iterating through a Dictionary squares = (1: 1, 3: 9, 5: 25, 7: 49, 9: 81) for i in squares: print(squares(i))

Output

 1 9 25 49 81

Dictionary Built-in Functions

में निर्मित कार्यों की तरह all(), any(), len(), cmp(), sorted(), आदि सामान्यतः शब्दकोशों के साथ उपयोग किया जाता है विभिन्न कार्य करने के लिए।

समारोह विवरण
सब() वापसी करें Trueयदि शब्दकोश की सभी कुंजियाँ सत्य हैं (या यदि शब्दकोश खाली है)।
कोई भी() Trueयदि शब्दकोश की कोई भी कुंजी सत्य है, तो वापस लौटें । यदि शब्दकोश खाली है, तो वापस लौटें False
लेन () शब्दकोश में लंबाई (आइटमों की संख्या) वापस करें।
सीएमपी () दो शब्दकोशों की वस्तुओं की तुलना करता है। (पायथन 3 में उपलब्ध नहीं)
क्रमबद्ध () शब्दकोश में कुंजियों की एक नई क्रमबद्ध सूची लौटाएं।

यहां कुछ उदाहरण दिए गए हैं जो शब्दकोश के साथ काम करने के लिए अंतर्निहित कार्यों का उपयोग करते हैं।

 # Dictionary Built-in Functions squares = (0: 0, 1: 1, 3: 9, 5: 25, 7: 49, 9: 81) # Output: False print(all(squares)) # Output: True print(any(squares)) # Output: 6 print(len(squares)) # Output: (0, 1, 3, 5, 7, 9) print(sorted(squares))

आउटपुट

 गलत सच 6 (0, 1, 3, 5, 7, 9)

दिलचस्प लेख...