अजगर सेट (उदाहरण के साथ)

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

वीडियो: पायथन में सेट

एक सेट वस्तुओं का एक अनियंत्रित संग्रह है। प्रत्येक सेट तत्व अद्वितीय है (कोई डुप्लिकेट नहीं) और अपरिवर्तनीय होना चाहिए (बदला नहीं जा सकता)।

हालाँकि, एक सेट अपने आप में परिवर्तनशील है। हम इसमें से आइटम जोड़ या हटा सकते हैं।

सेट का उपयोग गणितीय सेट संचालन जैसे कि संघ, प्रतिच्छेदन, सममित अंतर, आदि करने के लिए भी किया जा सकता है।

पायथन सेट बनाना

एक सेट घुंघराले ब्रेसिज़ के अंदर सभी वस्तुओं (तत्वों) को रखकर (), कॉमा द्वारा अलग किया जाता है, या अंतर्निहित set()फ़ंक्शन का उपयोग करके बनाया जाता है।

इसमें किसी भी संख्या में आइटम हो सकते हैं और वे विभिन्न प्रकार (पूर्णांक, फ्लोट, ट्यूपल, स्ट्रिंग आदि) के हो सकते हैं। लेकिन एक सेट में उसके तत्वों के रूप में सूची, सेट या शब्दकोश जैसे परस्पर तत्व नहीं हो सकते हैं।

 # Different types of sets in Python # set of integers my_set = (1, 2, 3) print(my_set) # set of mixed datatypes my_set = (1.0, "Hello", (1, 2, 3)) print(my_set)

आउटपुट

 (1, 2, 3) (1.0, (1, 2, 3), 'हैलो')

निम्नलिखित उदाहरणों को भी आज़माएँ।

 # set cannot have duplicates # Output: (1, 2, 3, 4) my_set = (1, 2, 3, 4, 3, 2) print(my_set) # we can make set from a list # Output: (1, 2, 3) my_set = set((1, 2, 3, 2)) print(my_set) # set cannot have mutable items # here (3, 4) is a mutable list # this will cause an error. my_set = (1, 2, (3, 4))

आउटपुट

 : 'सूची'

खाली सेट बनाना थोड़ा मुश्किल है।

खाली घुंघराले ब्रेसिज़ ()पायथन में एक खाली शब्दकोश बना देगा। बिना किसी तत्व के एक सेट बनाने के लिए, हम set()बिना किसी तर्क के फ़ंक्शन का उपयोग करते हैं ।

 # Distinguish set and dictionary while creating empty set # initialize a with () a = () # check data type of a print(type(a)) # initialize a with set() a = set() # check data type of a print(type(a))

आउटपुट

 

पायथन में एक सेट को संशोधित करना

सेट परस्पर भिन्न होते हैं। हालाँकि, चूंकि वे अनियंत्रित हैं, इसलिए अनुक्रमण का कोई अर्थ नहीं है।

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

हम add()विधि का उपयोग करके एक एकल तत्व जोड़ सकते हैं , और कई तत्व विधि का उपयोग कर सकते हैं update()update()विधि अपने तर्क के रूप में tuples, सूचियों, तार या अन्य सेट ले सकते हैं। सभी मामलों में, डुप्लिकेट से बचा जाता है।

 # initialize my_set my_set = (1, 3) print(my_set) # my_set(0) # if you uncomment the above line # you will get an error # TypeError: 'set' object does not support indexing # add an element # Output: (1, 2, 3) my_set.add(2) print(my_set) # add multiple elements # Output: (1, 2, 3, 4) my_set.update((2, 3, 4)) print(my_set) # add list and set # Output: (1, 2, 3, 4, 5, 6, 8) my_set.update((4, 5), (1, 6, 8)) print(my_set)

आउटपुट

 (1, 3) (1, 2, 3) (1, 2, 3, 4) (1, 2, 3, 4, 5, 6, 8)

एक सेट से तत्वों को निकालना

विधियों का उपयोग करके एक विशेष आइटम को सेट से हटाया जा सकता है discard()और remove()

दोनों के बीच एकमात्र अंतर यह है कि discard()फ़ंक्शन सेट को अपरिवर्तित छोड़ देता है यदि तत्व सेट में मौजूद नहीं है। दूसरी ओर, remove()फ़ंक्शन ऐसी स्थिति में एक त्रुटि बढ़ाएगा (यदि तत्व सेट में मौजूद नहीं है)।

निम्नलिखित उदाहरण इसे स्पष्ट करेगा।

 # Difference between discard() and remove() # initialize my_set my_set = (1, 3, 4, 5, 6) print(my_set) # discard an element # Output: (1, 3, 5, 6) my_set.discard(4) print(my_set) # remove an element # Output: (1, 3, 5) my_set.remove(6) print(my_set) # discard an element # not present in my_set # Output: (1, 3, 5) my_set.discard(2) print(my_set) # remove an element # not present in my_set # you will get an error. # Output: KeyError my_set.remove(2)

आउटपुट

 (1, 3, 4, 5, 6) (1, 3, 5, 6) (1, 3, 5) (1, 3, 5) ट्रेसबैक (सबसे हालिया कॉल अंतिम): फाइल "", लाइन 28, में कीरोर: २

इसी तरह, हम pop()विधि का उपयोग करके किसी आइटम को हटा और वापस कर सकते हैं ।

चूंकि सेट एक अनियंत्रित डेटा प्रकार है, इसलिए यह निर्धारित करने का कोई तरीका नहीं है कि किस आइटम को पॉप किया जाएगा। यह पूरी तरह से मनमाना है।

हम clear()विधि का उपयोग करके एक सेट से सभी आइटम भी निकाल सकते हैं ।

 # initialize my_set # Output: set of unique elements my_set = set("HelloWorld") print(my_set) # pop an element # Output: random element print(my_set.pop()) # pop another element my_set.pop() print(my_set) # clear my_set # Output: set() my_set.clear() print(my_set) print(my_set)

आउटपुट

 ('एच', 'एल', 'आर', 'डब्ल्यू', 'ओ', 'डी', 'ई') एच ('आर', 'डब्ल्यू', 'ओ', 'डी', 'ई' ) सेट()

पायथन सेट संचालन

सेटों का उपयोग गणितीय सेट संचालन जैसे संघ, चौराहे, अंतर और सममित अंतर को करने के लिए किया जा सकता है। हम इसे ऑपरेटरों या विधियों के साथ कर सकते हैं।

आइए हम निम्नलिखित कार्यों के लिए निम्नलिखित दो सेटों पर विचार करें।

 >>> A = (1, 2, 3, 4, 5) >>> B = (4, 5, 6, 7, 8)

यूनियन सेट करें

अजगर में संघ सेट करें

A और B का संघ दोनों सेटों से सभी तत्वों का एक समूह है।

|ऑपरेटर का उपयोग करके यूनियन का प्रदर्शन किया जाता है । union()विधि का उपयोग करके ही पूरा किया जा सकता है ।

 # Set union method # initialize A and B A = (1, 2, 3, 4, 5) B = (4, 5, 6, 7, 8) # use | operator # Output: (1, 2, 3, 4, 5, 6, 7, 8) print(A | B)

आउटपुट

 (1, 2, 3, 4, 5, 6, 7, 8)

पायथन शेल पर निम्नलिखित उदाहरणों का प्रयास करें।

 # use union function >>> A.union(B) (1, 2, 3, 4, 5, 6, 7, 8) # use union function on B >>> B.union(A) (1, 2, 3, 4, 5, 6, 7, 8)

अंतःकरण सेट करें

पायथन में अंतःशिरा सेट करें

ए और बी का अंतर एक तत्व का एक समूह है जो दोनों सेटों में आम है।

&ऑपरेटर का उपयोग करके अंतर्ग्रहण किया जाता है । intersection()विधि का उपयोग करके ही पूरा किया जा सकता है ।

 # Intersection of sets # initialize A and B A = (1, 2, 3, 4, 5) B = (4, 5, 6, 7, 8) # use & operator # Output: (4, 5) print(A & B)

आउटपुट

 (४, ५)

पायथन शेल पर निम्नलिखित उदाहरणों का प्रयास करें।

 # use intersection function on A >>> A.intersection(B) (4, 5) # use intersection function on B >>> B.intersection(A) (4, 5)

अंतर सेट करें

पायथन में अंतर सेट करें

सेट ए से सेट बी का अंतर ए (ए - बी) तत्वों का एक सेट है जो केवल ए में हैं, लेकिन बी में नहीं। इसी तरह, बी - ए बी में तत्वों का एक सेट है, लेकिन ए में नहीं।

अंतर -ऑपरेटर का उपयोग करके किया जाता है । difference()विधि का उपयोग करके ही पूरा किया जा सकता है ।

 # Difference of two sets # initialize A and B A = (1, 2, 3, 4, 5) B = (4, 5, 6, 7, 8) # use - operator on A # Output: (1, 2, 3) print(A - B)

आउटपुट

 (1, 2, 3)

पायथन शेल पर निम्नलिखित उदाहरणों का प्रयास करें।

 # use difference function on A >>> A.difference(B) (1, 2, 3) # use - operator on B >>> B - A (8, 6, 7) # use difference function on B >>> B.difference(A) (8, 6, 7)

सममित अंतर सेट करें

पायथन में सममितीय अंतर सेट करें

ए और बी के सममित अंतर ए और बी में तत्वों का एक सेट है, लेकिन दोनों में नहीं (चौराहे को छोड़कर)।

^ऑपरेटर का उपयोग करके सममित अंतर किया जाता है । विधि का उपयोग करके ही पूरा किया जा सकता है symmetric_difference()

 # Symmetric difference of two sets # initialize A and B A = (1, 2, 3, 4, 5) B = (4, 5, 6, 7, 8) # use operator # Output: (1, 2, 3, 6, 7, 8) print(A B)

आउटपुट

 (1, 2, 3, 6, 7, 8)

पायथन शेल पर निम्नलिखित उदाहरणों का प्रयास करें।

 # use symmetric_difference function on A >>> A.symmetric_difference(B) (1, 2, 3, 6, 7, 8) # use symmetric_difference function on B >>> B.symmetric_difference(A) (1, 2, 3, 6, 7, 8)

अन्य पायथन सेट के तरीके

There are many set methods, some of which we have already used above. Here is a list of all the methods that are available with the set objects:

Method Description
add() Adds an element to the set
clear() Removes all elements from the set
copy() Returns a copy of the set
difference() Returns the difference of two or more sets as a new set
difference_update() Removes all elements of another set from this set
discard() Removes an element from the set if it is a member. (Do nothing if the element is not in set)
intersection() Returns the intersection of two sets as a new set
intersection_update() Updates the set with the intersection of itself and another
isdisjoint() Returns True if two sets have a null intersection
issubset() Returns True if another set contains this set
issuperset() Returns True if this set contains another set
pop() Removes and returns an arbitrary set element. Raises KeyError if the set is empty
remove() Removes an element from the set. If the element is not a member, raises a KeyError
symmetric_difference() Returns the symmetric difference of two sets as a new set
symmetric_difference_update() Updates a set with the symmetric difference of itself and another
union() Returns the union of sets in a new set
update() Updates the set with the union of itself and others

Other Set Operations

Set Membership Test

We can test if an item exists in a set or not, using the in keyword.

 # in keyword in a set # initialize my_set my_set = set("apple") # check if 'a' is present # Output: True print('a' in my_set) # check if 'p' is present # Output: False print('p' not in my_set)

Output

 True False

Iterating Through a Set

We can iterate through each item in a set using a for loop.

 >>> for letter in set("apple"):… print(letter)… a p e l

Built-in Functions with Set

Built-in functions like all(), any(), enumerate(), len(), max(), min(), sorted(), sum() etc. are commonly used with sets to perform different tasks.

Function Description
all() Returns True if all elements of the set are true (or if the set is empty).
any() Returns True if any element of the set is true. If the set is empty, returns False.
enumerate() Returns an enumerate object. It contains the index and value for all the items of the set as a pair.
len() Returns the length (the number of items) in the set.
max() Returns the largest item in the set.
min() Returns the smallest item in the set.
sorted() Returns a new sorted list from elements in the set(does not sort the set itself).
sum() Returns the sum of all elements in the set.

Python Frozenset

फ्रोज़ेनसेट एक नया वर्ग है जिसमें एक सेट की विशेषताएं हैं, लेकिन एक बार असाइन किए जाने पर इसके तत्वों को नहीं बदला जा सकता है। जबकि टुपल्स अपरिवर्तनीय सूचियां हैं, फ्रोजेनसेट अपरिवर्तनीय सेट हैं।

परिवर्तनशील होने वाले सेट अप्राप्य होते हैं, इसलिए उन्हें शब्दकोश कुंजियों के रूप में उपयोग नहीं किया जा सकता है। दूसरी ओर, फ्रोज़ेनसेट्स धोने योग्य होते हैं और इन्हें एक शब्दकोश की कुंजी के रूप में इस्तेमाल किया जा सकता है।

फ्रोजेनसेट () फ़ंक्शन का उपयोग करके फ्रोज़ेनसेट्स बनाया जा सकता है।

यह डेटा प्रकार का समर्थन करता है तरीकों की तरह copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference()और union()। अपरिवर्तनीय होने के नाते, इसमें ऐसे तरीके नहीं हैं जो तत्वों को जोड़ते हैं या हटाते हैं।

 # Frozensets # initialize A and B A = frozenset((1, 2, 3, 4)) B = frozenset((3, 4, 5, 6))

पायथन शेल पर इन उदाहरणों का प्रयास करें।

 >>> A.isdisjoint(B) False >>> A.difference(B) frozenset((1, 2)) >>> A | B frozenset((1, 2, 3, 4, 5, 6)) >>> A.add(3)… AttributeError: 'frozenset' object has no attribute 'add'

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